Rails在观察者中拉动属性

时间:2012-11-12 05:51:14

标签: ruby-on-rails-3 observers

我有一个监视评论表的观察者,并会在创建内容后发送电子邮件。我似乎并没有在观察者中正确地进行关联,因为我收到错误或ArgumentError (wrong number of arguments (0 for 5)):使用此代码:

class CommentObserver < ActiveRecord::Observer
  def after_create(comment)

    if comment.guest_comment == true
      if comment.name?
        InvoiceMailer.message(comment.name, comment.content, comment.email, comment.user.email, comment.order_id).deliver
      elsif comment.email?
        # do something if there's no email yet
        InvoiceMailer.message(comment.name, comment.content, comment.order.customer.email, comment.user.email, comment.order_id).deliver
      else
        InvoiceMailer.message(comment.email, comment.content, comment.email, comment.user.email, comment.order_id).deliver
      end
    else
      # owner made the comment
      if comment.user.account.company_name?
        InvoiceMailer.message(comment.user.name+", "+comment.user.account.company_name, comment.content, comment.user.email, comment.email, comment.order_id).deliver
      else  
        InvoiceMailer.message(comment.user.name, comment.content, comment.user.email, comment.email, comment.order_id).deliver
      end
    end
  end
end

有没有办法在观察者中写出正确的关联?

1 个答案:

答案 0 :(得分:0)

所以我发现观察者没有/不应该访问数据库。我现在把我的逻辑移到了邮件程序(稍后将转移到帮助程序)。然后确保不要使用“message”作为您的方法名称,因为它是保留的;)