Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Actionmailer to email specific group of users based on User attribute being included in array
    text
    copied!<p>I have a <strong>Document</strong> model with a string column <strong>access</strong> and a <strong>User</strong> model with a string column <strong>team</strong>. In a <strong>Document</strong> I can enter an array of items for <strong>access</strong> such as "staff, sales, manager". Meanwhile, a <strong>User</strong> has only <strong>one</strong> item under <strong>team</strong>.</p> <p>I am trying to send emails to specific Users based on whether or not for each <strong>user.team</strong> is included in the array for <strong>Document.access</strong>.</p> <p><strong>For example</strong> if <code>Document.access = "sales, management"</code>, only Users with <code>team = "sales" or team = "management"</code> will be sent the email. Other Users such as team = "accounting" would not.</p> <hr> <h3>Update:</h3> <p>From what I gather on this <a href="https://stackoverflow.com/questions/5296687/rails-3-action-mailer-cannot-loop-to-send-emails">question</a>, It would appear ActionMailer cannot loop, so I have modified my document_observer. Now that I moved the loop portion out of the mailer, <strong>user.email</strong> is erroring out with </p> <p><code>undefined method 'user' for</code></p> <p>If I stick in a string like <strong>'joe@net.com'</strong> 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.</p> <p>Relevant code from my document_observer below:</p> <pre><code> 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 </code></pre> <p>Relevant code from mailer. How do I pass on <strong>user.email</strong> for each email?</p> <pre><code>def doc_notification(document) mail(:to =&gt; 'joe@net.com') end </code></pre> <hr> <h3>Latest Update:</h3> <p>Ok, I just switched a couple of lines to </p> <pre><code>MultiMailer.doc_notification(user).deliver </code></pre> <p>and</p> <pre><code>def doc_notification(user) </code></pre> <p>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..</p> <p><strong>I should also mention that there is no association between Document and User.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload