我有一个作业(由delayed_job
安排),当新用户注册到应用程序时,它会发送一封电子邮件。这是用户模型:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :country, :phone_number, :opt_in
def email=(email)
super
Delayed::Job.enqueue self
end
def perform
begin
UserMailer.welcome_email self
rescue => e
STDERR.puts "Cannot perform the mailer: #{e}"
end
end
end
负责注册新用户的操作是:
def create
user = User.new({:email => params[:email],
:password => params[:password],
:password_confirmation => params[:password_confirmation],
:country => params[:country],
:opt_in => Boolean(params[:opt_in]),
:phone_number => params[:phone_number]})
if user.save(:validate => false)
redirect_to wrap_users_path
end
end
当用户注册时,我会得到以下日志:
已启动的POST"/wrap_users"…在2011-05-30 12:58:35+0200 WrapUsersController处理#创建为HTML
参数:{"电子邮件"=>"xxxx","password"=>"[FILTERED]","password_conconfirmation"=>"[FILTERED]","phone_number"=>"xxx","opt_in"=>"true","country"=>"France"}
区域(0.3ms)插入"delayed_jobs"("优先级","尝试次数"、"处理程序"、"last_error","run_at"、"locked_at"one_answers"failed_at","locked_by","created_at","updated_at")值(0,0,'---!ruby/ActiveRecord:用户属性:
电子邮件:xxxxencrypted_password:$2a$10$DwHB/5zSp38xdAAIkYriSuwIiLyKy2geU5kLmzjz2f1WuxpqyIWreset_password_token:
reset_password_sent_at:
remember_created_at:sign_in_count:0 current_sign_in_at:
lastrongign_in_at:current_sign_in_ip:lastrongign_in_ip:created_at:
updated_at:国家:法国opt_in:真实电话号码:xxx’,无效,'2011-05-30 10:58:35.250464',NULL,NULL,NULL’2011年5月30日10:58:35.250560','2011-05-3010:58:35.250560')AREL(2.1ms)插入"用户"("电子邮件","encrypted_password","reset_password_token","reset_password_sent_at","remember_created_at","sign_in_count"、"current_sign_in_at","lastrongign_in_at","current_sign_in_ip","lastrongign_in_ip","created_at","updated_at","country","opt_in","phone_number")值("xxx",'$2a$10$DwHB/5zSp38xdAAIkYriSuwIiLyKy2geU5kLmzjz2f1WuxpqyIW',NULL,NULL,NULL,无效,'2011-05-30 10:58:35.251788',‘2011-05-30 10:58:35.251788’,"法国"、"t"、"xxx")已重定向至http://...:3000/wrap_users完成302在277ms 中找到
正如您所看到的,根据Log,一个条目被记录到Delayed::Job
表中。但事实上,当计算该表中记录的数量时(在相同开发模式下的rails console
上),我得到0。
发生了什么事?日志没有提到任何关于insert into delayed_jobs
步骤的问题。
感谢
表为空,可能是因为某个工作人员将作业从表中删除。顺便说一句,我会使用after_create回调而不是email=来安排作业。至少有一个原因——即使验证失败,你也会发送电子邮件。