如何检索属性== self.id的行并向self.email发送电子邮件

时间:2014-12-16 16:27:15

标签: ruby-on-rails find rows

看起来一定很简单,但是我已经两天搞乱这个并且无法让它发挥作用。 感谢您提前提供任何帮助。

我有1个表,其中包含self.id,self.number和self.email等不相关的表。

(第一集)有些行有self.id和self.number (第二组)其他行有self.id和self.email

我想更新第一组中的一行,然后找到第二组中self.id等于self.number的所有行,向他们发送电子邮件。

我最后一次尝试的是通过这样的范围:

 scope :send_alert, (lambda do |self_number| 
   { :conditions => ['self_number = ?', self_id] }
 end)

 scope :send_alert2, (lambda do |self_id| 
   { :conditions => ['self_id = ?', self_number] }
 end)

我没有得到错误,一切都顺利但没有任何反应 我正在使用delayed_job,我发现我发送的行刚刚更新,但不是我想要的那些。

1 个答案:

答案 0 :(得分:0)

您可以使用条件回调来完成此操作

class MyModel < ActiveRecord::Base

  # checks if the model has the attribute 'number' and calls send_emails func
  after_update :send_emails, if: :number

  # iterate through where the id matches the given number and call your alert fucntion
  def send_emails
    MyModel.find_by_id(:number).map {|m| m.send_email(m.email)} 
  end

end