Umang Raghuvanshi
Umang Raghuvanshi

Reputation: 1258

Rails convert memory address to object

I have a bit of code that calls a Sidekiq worker like this. NotifySubscriberWorker.perform_async(@incident).

@incident is a object of class Incident (which is a model). However, in my mailer, when I try to perform operations like @incident.created_at, this is what I get. ActionView::Template::Error: undefined method 'updated_at' for "#<Incident:0x007f31ccd5f020>":String

I'm assuming that for some reason @incident isn't being passed around as an Incident. Here's my worker's code.

class NotifySubscriberWorker
  include Sidekiq::Worker

  def perform(incident)
    # Notify all activated subscribers when there's an update to an incident
    Subscriber.where(activated: true).find_each do |subscriber|
      # Schedule the mail
      SubscriberMailer.notify_subscriber(subscriber, incident)
    end
  end
end

Here's the mailer's code.

class SubscriberMailer < ApplicationMailer
  @@app_name = APP_CONFIG['name']
  def activate_subscriber(subscriber)
    @activation_url = root_url + "subscribers/activate/#{subscriber.activation_key}"
    mail to: subscriber.email, subject: "Please confirm your subscription for #{@@app_name}'s incidents."
  end
  def notify_subscriber(subscriber, incident)
    @incident = incident
    mail to: subscriber.email, subject: "There is an update to #{@@app_name}'s status.'"
  end
end

The view attempts to access @incident from the definition above.

Thanks in advance.

Upvotes: 1

Views: 373

Answers (1)

caspg
caspg

Reputation: 399

I think you should pass an object_id instead of whole object to your worker.

Call worker:

NotifySubscriberWorker.perform_async(@incident.id)

Then change in your worker:

SubscriberMailer.notify_subscriber(subscriber, incident_id)

and then in mailer:

  def notify_subscriber(subscriber, incident_id)
    @incident = Incident.find(incident_id)
    mail to: subscriber.email, subject: "There is an update to #{@@app_name}'s status.'"
  end

Upvotes: 1

Related Questions