Rails Mailer:根据匹配条件向收件人发送电子邮件

Rails Mailer: send emails to recipients based on matching criteria

我已经成功配置我的 ActionMailer 以根据新 'submission' 向收件人发送电子邮件。但是,我的应用程序的工作方式是它采用提交 'Desired Location' 字段并将其与另一个名为 Agents 的模型中的 'Company Business location' 字段匹配,以提供根据用户按位置匹配的索引视图。也就是说,如果我提交了一个位置为伦敦的提交,那么只有位置为伦敦的代理才能看到它。这让我想到了我的电子邮件,是否可以创建一个以相同方式工作的邮件程序?因此,只向符合所需提交位置的代理发送电子邮件?

邮寄者

class NewSubmissionMailer < ApplicationMailer

  def submission_email(submission)
    @submission = submission
    mail(to: @submission.Email, subject: 'Welcome to Ottom8')
  end

end

提交控制器

respond_to do |format|
  if @submission.save

    # Tell the UserMailer to send a welcome email after save       
    NewSubmissionMailer.submission_email(@submission).deliver_now

匹配两个模型的代码

def index
  @submissions = Submission.where(:Desired_Location => current_agent.Company_Business_Location)
end

谢谢

respond_to do |format|
  if @submission.save
    # Tell the UserMailer to send a welcome email after save
    NewSubmissionMailer.submission_email(@submission).deliver_now

    # Send emails to matching agents
    NewSubmissionMailer.matching_agents_email(@submission).deliver_now

然后在邮件中 ::matching_agents_email:

def matching_agents_email(submission) 
  @submission = submission
  agents = Agent.where(:Company_Business_Location => @submission.Desired_Location)
  mail(to: agents.pluck(:email) # ... Rest of email logic. )