I'm trying to hook a mailer before my User object is destroyed.
I am using Sidekiq to send emails but this error comes out when the User is destroyed.
ActiveJob::DeserializationError: Error while trying to deserialize arguments: Couldn't find User with id [id]
In my model I have this:
class User < ApplicationRecord
before_destroy :goodbye_user
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable
private
def send_devise_notification(notification, *args)
devise_mailer.send(notification, self, *args).deliver_later
end
def goodbye_user
GoodbyeUser.goodbye_user(self).deliver_later
end
end
And farewell user mail never sends.
Any idea?
It's because you're sending the email asynchronously via deliver_later
. Since it's on a separate thread, by the time the worker gets the job the user is already destroyed.
You can either send the email synchronously or don't use a before_destroy
callback. If you'd like to send the email in the background, just call your goodbye_user
method and destroy the user there (after the email is sent).