Car*_*mez 1 email ruby-on-rails sidekiq rails-activejob
我试图在我的User对象被销毁之前挂钩邮件程序.
我正在使用Sidekiq发送电子邮件,但是当用户被销毁时会出现此错误.
ActiveJob::DeserializationError: Error while trying to deserialize arguments: Couldn't find User with id [id]
在我的模型中,我有这个:
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
Run Code Online (Sandbox Code Playgroud)
告别用户邮件永远不会发送.
任何的想法?
这是因为您通过异步发送电子邮件deliver_later.由于它位于一个单独的线程上,所以当工作者获得该作业时,该用户已被销毁.
您可以同步发送电子邮件,也可以不使用before_destroy回调.如果您想在后台发送电子邮件,只需调用您的goodbye_user方法并在那里销毁用户(在发送电子邮件之后).