Rails将内存地址转换为对象

时间:2015-10-12 15:28:49

标签: ruby-on-rails ruby

我有一些代码调用像这样的Sidekiq工作者。 NotifySubscriberWorker.perform_async(@incident)

@incident是类Incident的对象(它是一个模型)。但是,在我的邮件程序中,当我尝试执行@incident.created_at之类的操作时,这就是我得到的。 ActionView::Template::Error: undefined method 'updated_at' for "#<Incident:0x007f31ccd5f020>":String

我假设由于某种原因@incident没有作为事件传递。这是我工人的代码。

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

这是邮寄者的代码。

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

该视图尝试从上面的定义中访问@incident

提前致谢。

1 个答案:

答案 0 :(得分:1)

我认为你应该将object_id而不是整个对象传递给你的工作者。

致电工作人员:

NotifySubscriberWorker.perform_async(@incident.id)

然后改变你的工人:

SubscriberMailer.notify_subscriber(subscriber, incident_id)

然后在邮件中:

  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