Reputation: 37
Using devise, I'm trying to set a custom attribute automatically after a user is created.
Here is what I have in my User model.
after_save :set_attribute_to_devise_id
private
def set_attribute_to_devise_id
self.custom_attribute = self.id
end
When a user registers and an account is created, the custom_attribute is not saving the ID that devise generated. However, when testing in the console, running User.save on the user seems to update the custom_attribute, but does not persist or save to the database permanently.
I've tried after_commit as well as after_create without any luck. How do I save the user.id from Devise automatically to another attribute column on user creation?
Upvotes: 0
Views: 610
Reputation: 3347
The callback should be before_save
at max.
after_save
is run when all the changes have been saved into the database, so in this case it is too late.
Upvotes: 2