I am a NOOB trying to work with delayed_job.
I want to update a User Model after the mail is successfully sent using delayed job.
Send email:
UserMailer.delay.welcome_email(user)
if mail sent successfully do the following:
User.update_attributes(:emailed => true)
How can I get a callback or trigger when the email is successfully sent?
You need to create a Job object instead of calling the #delay
helper. You can use the success
hook to execute the callback.
class WelcomeEmailJob < Struct.new(:user_id)
def perform
UserMailer.welcome_email(user)
end
def success(job)
user.update_attribute(:emailed, true)
end
private
def user
@user ||= User.find(user_id)
end
end
Delayed::Job.enqueue WelcomeEmailJob.new(user.id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With