Resetting password of Devise user from the database

I recently encountered a situation where I was trying to access a Rails application but:

  • I didn’t know the password for the admin user
  • The reset password functionality was not working
  • I had no access to a Rails console

Knowing the User model was using Devise, I set a password for a user on a dev environment to find the encrypted_password:

u = User.find(1)
u.update(password: 'password')
u.encrpyted_password
=> "$2a$10$qjBArbMLISBaUDMKdNM0KuZNPsRzPMIINMsPT.NhE9UrIYnErVF2S"

I could then use SQL to update the password on the prod environment:
UPDATE users SET encrypted_password = '$2a$10$qjBArbMLISBaUDMKdNM0KuZNPsRzPMIINMsPT.NhE9UrIYnErVF2S' WHERE id = 1;