如何调试这个“无方法”? ruby on Rails 错误?

发布于 2024-09-24 01:32:12 字数 1900 浏览 0 评论 0原文

我收到以下错误:

Delayed::Job SomeMailJob# (NoMethodError) "undefined method `subject' for #<YAML::Object:0x2b0a191f4c78>"

这来自引用上面的 SombMailJob 的以下代码:

 class SomeMailJob < Struct.new(:contact, :contact_email) 
   def perform
     OutboundMailer.deliver_campaign_email(contact,contact_email)
   end
 end

这是邮件程序:

class OutboundMailer < Postage::Mailer 

  def campaign_email(contact,email)
    subject    email.subject
    recipients contact.email
    from       'me.com>'
    sent_on    Date.today

    body       :email => email
  end

这是调用邮件程序的 cron 任务:

Contact.all.each do |contact|
  email = contact.email_today #email_today is a contact method returning email object if <= today

  unless contact.email_today == "none" || email.nil?
    puts "contact info inside cron job"
    puts contact.first_name
    puts email.days
    puts contact.date_entered
    puts contact.colleagues
    puts "substituted subject:"
    puts email.substituted_subject(contact,contact.colleagues)

    # create the Contact Email object that gets created and sent

    contact_email = ContactEmail.new
    contact_email.contact_id = contact.id
    contact_email.email_id = email.id

    contact_email.subject = email.substituted_subject(contact,contact.colleagues)


    puts contact_email.subject

    contact_email.date_sent = Date.today
    contact_email.date_created = Date.today

    contact_email.body = email.substituted_message(contact, contact.colleagues)

    contact_email.status = "sent" 

    #Delayed::Job.enqueue OutboundMailer.deliver_campaign_email(contact,contact_email)
    Delayed::Job.enqueue SomeMailJob.new(contact,contact_email)

    contact_email.save #now save the record

问题:为什么我会收到此错误?我什至不知道该对象是什么,因为它是与代码一起出现的,所以我无法真正进一步深入进行调试。

I get the following error:

Delayed::Job SomeMailJob# (NoMethodError) "undefined method `subject' for #<YAML::Object:0x2b0a191f4c78>"

This comes from the following code which references the SombMailJob above:

 class SomeMailJob < Struct.new(:contact, :contact_email) 
   def perform
     OutboundMailer.deliver_campaign_email(contact,contact_email)
   end
 end

Here is the mailer:

class OutboundMailer < Postage::Mailer 

  def campaign_email(contact,email)
    subject    email.subject
    recipients contact.email
    from       'me.com>'
    sent_on    Date.today

    body       :email => email
  end

This is the cron task that invokes the mailer:

Contact.all.each do |contact|
  email = contact.email_today #email_today is a contact method returning email object if <= today

  unless contact.email_today == "none" || email.nil?
    puts "contact info inside cron job"
    puts contact.first_name
    puts email.days
    puts contact.date_entered
    puts contact.colleagues
    puts "substituted subject:"
    puts email.substituted_subject(contact,contact.colleagues)

    # create the Contact Email object that gets created and sent

    contact_email = ContactEmail.new
    contact_email.contact_id = contact.id
    contact_email.email_id = email.id

    contact_email.subject = email.substituted_subject(contact,contact.colleagues)


    puts contact_email.subject

    contact_email.date_sent = Date.today
    contact_email.date_created = Date.today

    contact_email.body = email.substituted_message(contact, contact.colleagues)

    contact_email.status = "sent" 

    #Delayed::Job.enqueue OutboundMailer.deliver_campaign_email(contact,contact_email)
    Delayed::Job.enqueue SomeMailJob.new(contact,contact_email)

    contact_email.save #now save the record

Question: why am I getting this error? I don't even know what the object is because it is coming up with the code, so I can't really drill-down further to debug.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-10-01 01:32:12

这对我有用。看来和你遇到的问题是一样的。

Rails 延迟作业和延迟作业库类

This worked for me. Seems to be the same issue you're having.

Rails Delayed Job & Library Class

于我来说 2024-10-01 01:32:12

当您对延迟作业进行排队时,它会将所涉及的内容(类、您调用的方法的名称、参数)序列化到 YAML 中,以便稍后在运行作业时将它们拉出并使用它们。

在您的情况下,在调用 .subject 之前,email 参数似乎没有从 YAML 中正确反序列化。

我发现,delayed_job 在序列化/反序列化任何不是简单存储的 ActiveRecord 对象或基本类型(整数、字符串)的内容时往往会遇到问题。我总是尝试进行设置,以便我的 Job 对象仅采用记录 ID(整数),然后在执行方法中找到对象并使用它们。这绝对可以避免您遇到的麻烦。

When you enqueue a delayed job, it serializes the things involved (the class, the name of hte method you're calling on it, the arguments) into YAML so it can pull them out later when running the job and work with them.

It looks like in your case the email argument is not getting deserialized properly from YAML before .subject is called on it.

I've found that delayed_job tends to have trouble serializing/deserializing anything that's not a simple stored ActiveRecord object or primitive type (integer, string). I always try to set things up so my Job objects only take record IDs (integers), then in the perform method I find for the objects there and work with them. This would definitely avoid the trouble you're seeing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文