循环工作,但邮件不

我有一种方法,基本上会发出通知,告知有人在我的应用中提交了表单。 我最近更改了我的模型,以便通过添加user_designations模型来处理所有分配(用户被分配到哪个学校等)来通知多个人。

方法:

def new_message(applicant) @applicant = applicant @applicant.school.users.each do |user| mail(:to => user.email, :subject => "Submitted Application") end end 

对象:

 class Applicant belongs_to :school class School has_many :applicants has_many :user_designations has_many :users, :through => :user_designations class User has_many :schools, :through => :user_designations has_many :applicants, :through => :schools 

邮件function仅适用于循环的最后一次迭代。 我也收到一个错误:

#School的未定义方法`user’:0x007fe064700890

基于这些少量信息的任何想法?

在ActionMailer子类中,每个邮件程序操作在调用时构造邮件消息的内部表示,以供Rails传递。 在你的循环中,你只是一遍又一遍地重建同一个邮件消息,所以只有循环的最后一次迭代才能存活 – 这就像ActionMailer的编写方式一样。

如果您想向多个收件人发送邮件,您可以选择以下几种方式:

  • 使用你正在调用邮件程序的循环( Mailer.new_message(...).deliver
  • 在邮件程序中的To / CC / BCC中使用多个地址