1

我有两张桌子:PeopleCar

  • Person has_many cars
  • Car belongs_to person.

license_plate我想在汽车更换时向该人发送电子邮件。

我已成功制作邮件代码,但在回调中设置if条件时遇到问题。after_save

#Inside Person models
after_save :send_mail_notification, if: (self.cars.first.order('updated_at DESC').changed?)

def send_mail_notification(person)
    ...
end

我收到了这个错误

NoMethodError: undefined method `cars' for #<Class:0x4852ba8>

所以,我想self在回调中不可用?有什么解决办法吗?

谢谢

4

2 回答 2

4
after_save :send_mail_notification, if: Proc.new { |model| model.cars.order('updated_at DESC').first.changed? }
于 2013-04-01T14:45:54.177 回答
1

将其移入方法中?

after_save :send_mail_notification

def send_mail_notification(person)
    if (person.cars.first.order('updated_at DESC').changed?
      ..
    end
end
于 2013-04-01T14:46:42.900 回答