Question
How can I change and confirm the email of a user from code?
I need to change the email for a user manually and confirm it.
I use Devise with the reconfirmable module.
I tried to change the email attribute normally and save the record, but the email does not change:
user = User.find_by email: 'old@example.com'
user.email = 'new@example.com'
user.save
user.email # => 'old@example.com'
Answer
What you are describing is caused by the reconfirmable module.
The new email is stored in the unconfirmed_email
attribute.
You simply need to confirm
the change manually, so that the new email is moved from the unconfirmed_email
attribute to the email
attribute:
user.confirm
user.email # => 'new@example.com'