Reputation: 1127
I have very strange behaviour when I'm initialising new User.
I run code like @user = User.where(:provider => @provider, :uid => @shop[:uid]).first_or_initialize
setting all neded data and then saving @user.save
But then when I pass user id immediately after saving to Worker
EmailWelcome.perform_async @user.id
It passes nil
as user id.. So my question has anybody seen such behaviour with *_initialize methods and what should I do?
EDIT: Also need to mention, that it saves record in DB
Upvotes: 1
Views: 518
Reputation: 670
I was just looking for an answer to the same problem. I have multiple databases and it was working on dev but not staging.
The id column on the staging column was missing the auto-increment sequence (in postgres).
Something else for you to check.
HTH
Upvotes: 0
Reputation: 6950
Are you sure that the id is nil when it's being sent to the worker? Or that the worker process just can't find the user based on the id that's given to the job?
If you are using a worker process like resque or sidekiq, it won't have access to the user until the transaction completes. If that's the problem, you need to use after_commit
or some other way to queue up the EmailWelcome
job after the transaction completes.
Upvotes: 1
Reputation: 160843
You have to check whether the user is successfully saved.
@user = User.where(:provider => @provider, :uid => @shop[:uid]).first_or_initialize
if @user.save
EmailWelcome.perform_async @user.id
else
p @user.errors
end
Or using @user.save!
, this way will throw exception if save failed.
Upvotes: 0