I've written my first rake task in rails, I've set it up with the Heroku scheduler and it is getting run, however the mail isn't getting sent. My mail settings are fine as I'm using it for various other things, I would imagine it's a problem with my code in the rake task. Any help would be much appreciated.
desc "Remind users if they haven't completed quote form"
task uncomplete_form: :environment do
puts 'Reminding users of uncomplete quote form'
date = Date.parse('december 18 2016')
quickcontacts = Quickcontact.where(created_at: date.midnight..Time.now)
quickcontacts.each do |quickcontact|
next unless quickcontact.created_at > 1.hour.ago
if quickcontact.p_p = nil
QuickcontactMailer.uncomplete_form(@quickcontact).deliver
end
end
puts 'done.'
end
By running rake uncomplete_form I get
Reminding users of uncomplete quote form
done.
And running heroku run rake uncomplete_form I get
Reminding users of uncomplete quote form
Quickcontact Load (1.5ms) SELECT "quickcontacts".* FROM "quickcontacts" WHERE ("quickcontacts"."created_at" BETWEEN '2016-12-18 00:00:00.000000' AND '2016-12-20 12:09:23.683977')
done.
It doesn't seem to be picking up any quickcontacts - however if in the console I run:
date = Date.parse('december 18 2016')
followed by
quickcontacts = Quickcontact.where(created_at: date.midnight..Time.now)
It does find the expected contacts
Have you tried
QuickcontactMailer.uncomplete_form(@quickcontact).deliver_now
?
Edit: What's .p_p supposed to be? You're doing assignment in that if clause instead of what I presume should have been comparison (?)
if quickcontact.p_p = nil
Solved: Two bad errors on my part. Thanks for the help with the first @matija. The problem was with my rake task code. The first error was where I had 'if quickcontact.p_p = nil' needed to be changed to 'if quickcontact.p_p.nil?' - I was accidentally assigning nil value, rather than checking it. The second error was that in the next line down, quickcontact should not have been an instance variable. This is the updated, functioning code:
desc "Remind users if they haven't completed quote form"
task uncomplete_form: :environment do
puts 'Reminding users of uncomplete quote form'
date = Date.parse('december 18 2016')
quickcontacts = Quickcontact.where(created_at: date.midnight..Time.now)
quickcontacts.each do |quickcontact|
next unless quickcontact.created_at > 1.hour.ago
if quickcontact.p_p.nil?
QuickcontactMailer.uncomplete_form(quickcontact).deliver
end
end
puts 'done.'
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.