Rails Actionmailer根据数组中包含的User属性通过电子邮件发送特定的用户组 [英] Rails Actionmailer to email specific group of users based on User attribute being included in array

查看:95
本文介绍了Rails Actionmailer根据数组中包含的User属性通过电子邮件发送特定的用户组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有字符串列 access Document 模型和一个带有 team 字符串列的 User 模型。在文档中,我可以输入用于访问的一系列项目,例如工作人员,销售人员,经理。同时,用户团队下只有一个项目。

I have a Document model with a string column access and a User model with a string column team. In a Document I can enter an array of items for access such as "staff, sales, manager". Meanwhile, a User has only one item under team.

我正在尝试根据是否每个 user.team 是否包含在 Document.access 的数组中,向特定用户发送电子邮件。

I am trying to send emails to specific Users based on whether or not for each user.team is included in the array for Document.access.

例如,如果 Document.access =销售,管理 ,则只有拥有 team =的用户销售或团队=管理 会收到电子邮件。

For example if Document.access = "sales, management", only Users with team = "sales" or team = "management" will be sent the email. Other Users such as team = "accounting" would not.

根据我在此问题,看来ActionMailer无法循环,所以我修改了document_observer。现在,我将循环部分移出了邮件程序, user.email 正在使用

From what I gather on this question, It would appear ActionMailer cannot loop, so I have modified my document_observer. Now that I moved the loop portion out of the mailer, user.email is erroring out with

未定义方法'用户的

如果我输入'joe@net.com'之类的字符串,电子邮件发送出去,因此该部分似乎起作用。现在,只需将正确的电子邮件地址传递给邮件即可。

If I stick in a string like 'joe@net.com' the proper number of emails go out, so that part seems to work. Now it is a matter of passing the right email addresses to the message.

下面来自我的document_observer的相关代码:

Relevant code from my document_observer below:

 def after_save(model)
   @users = User.all
   @users.each do |user|
     if model.access.include? user.team
       MultiMailer.doc_notification(model).deliver
     end
   end  
 end

邮件程序中的相关代码。如何为每封电子邮件传递 user.email

Relevant code from mailer. How do I pass on user.email for each email?

def doc_notification(document)
  mail(:to => 'joe@net.com')
end






最新更新:



好,我只是将几行切换为


Latest Update:

Ok, I just switched a couple of lines to

MultiMailer.doc_notification(user).deliver

def doc_notification(user)

因此,现在电子邮件发给了每个合适的用户,但看来我只是在扭转上述问题,并将未知用户换成未知文档。电子邮件需要引用刚刚更新的文档的URL。

So now the email goes out to each of the right users, but it appears I am just reversing the problem above and trading unknown user for unknown document. The email needs to reference the URL for the document that was just updated..

我还应该提到文档和用户之间没有关联。

I should also mention that there is no association between Document and User.

推荐答案




答案



来自 document_observer

def after_save(model)
  @users = User.all
  @users.each do |user|
    if model.access.include? user.team
      MultiMailer.doc_notification(model, user).deliver
    end
  end
end

来自邮件程序

def doc_notification(document, user)
  @document = document
  mail(:to => user.email)
end

信用额度非常好 Nathaniel Talbott 三角红宝石旅亲自回答了我的问题。我认为有时走出房子很有帮助。

Credit goes to a very nice Nathaniel Talbott of Triangle Ruby Brigade who answered my question in person. I guess it helps to get out the house sometimes.

这篇关于Rails Actionmailer根据数组中包含的User属性通过电子邮件发送特定的用户组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆