How can I deliver_now
conditionally based on the content of the data returned by the mailer method?
I have a Mailer that is sent via a loop through a list of users, like so:
users.each do |u|
AbcMailer.with(user_id: u.user_id).abc_report.deliver_now
end
The list of users that should receive the mailer (users
in the loop) lives in ActiveRecord, and all the users' actual data lives in an external MySql DB.
The abc_report
method in the AbcMailer
class makes some queries to the MySql DB and returns a bunch of info for each user, which is then inserted into an html.erb email template, and delivered now.
My issue is that I need to only deliver to some of those users, because in the DB, one of the pieces of info that comes back is whether the user is active or not. So I would like to only deliver_now
if the user has active = 1. But I can't find any examples of unchaining these methods to do what I want.
When I just do AbcMailer.with(user_id: u.user_id).abc_report
, what it returns is actually the filled-out html.erb template already. When I do AbcMailer.with(user_id: u.user_id)
by itself, it returns #<ActionMailer::Parameterized::Mailer:0x00007fdd43eb5528>
.
Things I've Tried
I tried inserting a return if user["Active"] == 0
in the abc_report
method but that obviously killed the entire loop rather than skipping to the next item, so I'm working under the assumption that the skip has to happen with a next
in the actual loop itself, not in an external method being called.
I also found this which seems like a great solution in a plain Ruby context but because in this case, abc_report
is automatically filling out and returning the AbcMailer
html.erb template...I'm stumped on how I would get it to just return a boolean without killing the whole loop.