Reputation: 299
I have defined a function which sends email to 200 parents when we click on send invitation email. The issue I am facing is when I click on send invitation email i get a message "Invitation email have already been sent to all". But there are many parents to whom email has not been sent.
parents_controller.rb
def send_email_to_everyone
@parents = Parent.where(invitation_email_sent: false, admin: false, email: !nil).limit(200)
# puts @parents.length
if @parents.length > 0
@parents.each do |parent|
parent.create_reset_digest
ParentMailer.invitation_confirmation(parent).deliver
parent.update_attributes(invitation_email_sent: true)
end
flash[:success] = "Invitation email has been sent to all."
redirect_to main_admin_path
else
flash[:success] = "Invitation email have already been sent to all"
redirect_to main_admin_path
end
end
main_admin.html.erb
<%= link_to "Send Invitation Email", send_email_to_everyone_path, class: "btn btn-info btn-2x" %>
Upvotes: 1
Views: 54
Reputation: 4093
The problem is in the way you select your parents, you write email: !nil
.
In rails !nil
is true
.
So you are looking for parents whose email is the value true
. You should write :
@parents = Parent.where(invitation_email_sent: false, admin: false).where.not(email: nil).limit(200)
I replaced
.where(email: !nil)
with .where.not(email: nil)
Upvotes: 1