Heroku rake task not loading ActionMailer class
I have a rake task that delivers daily emails via ActionMailer. My plan is to use Heroku's cron job to run this task nightly.
The problem is that my ActionMailer class (Notifier) is not recognized with Heroku. The specific error is "Unitialized constant Notifier." However, the Notifier class properly sends out emails in Heroku everywhere else in the application (controllers and models) but not from the rake task. Running the rake task also does work locally.
If I d开发者_运维技巧o heroku rake cron to run the task manually, it throws that error.
heroku rake cron
This doesn't work. However,
rake RAILS_ENV=production cron
works fine (Heroku vs. local).
I've even tried adding my mailer path to the autoload directory.
I think in this case seeing your code would be helpful. I implemented the same exact thing this past weekend on heroku and had no problems. For me my rake task calls a method on my model like so...
#note this has to be in lib/tasks and be named cron.rake
desc "Send mailing"
task :cron => :environment do
Lease.updates
end
My model has the following method...
def self.updates
//setting up params
UserMailer.deliver_report_due(user, @leases)
end
This will call deliver on your method in your UserMailer class.
def report_due(user, leases)
recipients user.email
from "email@email.com"
subject "Confirmation"
body :leases=>leases,
:user=>user
end
Also note that this is rails 2.3 in which you call UserMailer.deliver_method, In rails 3 I think it is UserMailer.method_deliver.
If all else fails you could try restarting your heroku app on the command line with 'heroku restart'
Heroku finally figured out what was going on. I originally created my Notifier class file named as "Notifier" (capitalized first letter) instead of "notifier". I realized the mistake and manually changed the filename, however I had already did a git commit. Well since Max OS X is case insensitive for filenames, changing the capitalization to lowercase didn't commit to git. So everything worked locally on my Macbook, and showed that the filename was correct, but deploying to Heroku pushed a improperly named file. Even worse is that it only was affected under the rake tasks.
I don't think I would have ever figured this out without Heroku.
Try to write
require 'notifier'
in your rake task file
精彩评论