21st
Rails 3 ActionMailer and Delayed Job
What I’d like to do:
message = Notifier.password_reset_instructions(self)
Delayed::Job.enqueue MailerJob.new(message)
What happens when I do:
TypeError in Public/password resetsController#create
can’t dump anonymous class Class
…
/lib/delayed/backend/base.rb:61:in `payload_object=’
Problem line 61:
self[‘handler’] = object.to_yaml
Console confirms:
> user = User.find(:first)
> message = Notifier.password_reset_instructions(user)
> message.to_yaml
TypeError: can’t dump anonymous class Class
How I fixed it. Note: all of my notifier methods take a user argument, so I thought this was a little cleaner than coming up with a new job class for every email method:
Delayed::Job.enqueue NotifierJob.new(:password_reset_instructions,user_id)
And my lib/notifier_job.rb file:
class NotifierJob < Struct.new(:notifier_method,:user_id)
def perform
user = User.find(user_id)
Notifier.send(notifier_method,user).deliver
end
end
Hope that helps.