I have a job (scheduled by delayed_job) that sends an email when a new user registers to the application. This is the user model:
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
The action responsible for registering a new user is:
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
When a user registers I get the following log:
Started POST “/wrap_users” for
… at 2011-05-30 12:58:35 +0200 Processing by WrapUsersController#create as HTMLParameters:
{“email”=>”xxxx”,
“password”=>”[FILTERED]”,
“password_confirmation”=>”[FILTERED]”,
“phone_number”=>”xxx”,
“opt_in”=>”true”, “country”=>”France”}AREL (0.3ms) INSERT INTO
“delayed_jobs” (“priority”,
“attempts”, “handler”, “last_error”,
“run_at”, “locked_at”, “failed_at”,
“locked_by”, “created_at”,
“updated_at”) VALUES (0, 0, ‘—
!ruby/ActiveRecord:User attributes:
email: xxxx
encrypted_password:
$2a$10$DwHB/5zSp38xdAAIkYriSuwIiLyKy2geU5kLmzjz2f1WsAuxpqyIW
reset_password_token:
reset_password_sent_at:
remember_created_at: sign_in_count:
0 current_sign_in_at:
last_sign_in_at: current_sign_in_ip:
last_sign_in_ip: created_at:
updated_at: country: France opt_in:
true phone_number: xxx ‘,
NULL, ‘2011-05-30 10:58:35.250464’,
NULL, NULL, NULL, ‘2011-05-30
10:58:35.250560’, ‘2011-05-30
10:58:35.250560’) AREL (2.1ms)INSERT INTO “users” (“email”,
“encrypted_password”,
“reset_password_token”,
“reset_password_sent_at”,
“remember_created_at”,
“sign_in_count”, “current_sign_in_at”,
“last_sign_in_at”,
“current_sign_in_ip”,
“last_sign_in_ip”, “created_at”,
“updated_at”, “country”, “opt_in”,
“phone_number”) VALUES
(‘xxx’,
‘$2a$10$DwHB/5zSp38xdAAIkYriSuwIiLyKy2geU5kLmzjz2f1WsAuxpqyIW’,
NULL, NULL, NULL, 0, NULL, NULL, NULL,
NULL, ‘2011-05-30 10:58:35.251788’,
‘2011-05-30 10:58:35.251788’,
‘France’, ‘t’, ‘xxx’)
Redirected to
http://…:3000/wrap_users
Completed 302 Found in 277ms
As you can see, according to the Log an entry is being recorded to the Delayed::Job table. But in fact, when counting the number for records in this table (on rails console in the same development mode) I get 0.
What is going on? The log didn’t mention any issue regarding the insert into delayed_jobs step.
Thanks
The table is empty probably because a worker took the job out of there and deleted it.
As a side note, I would use after_create callback instead of email= for scheduling a job. At least for one reason – even if the validation fails, you’ll send the email anyway.