115

While running an app how do you select a user by email address and then set the password manually within rails console for Devise?

Also, where would I go to review documentation to cover more details in this regard to manipulation of accounts while using Devise?

ylluminate
  • 12,102
  • 17
  • 78
  • 152

8 Answers8

183

Modern devise allows simpler syntax, no need to set the confirmation field

user.password = new_password; user.save
# or
user.update(password: new_password)
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • Ah, hmm. That would work for a standard user, but in this case it's from the admin_users table. What's the appropriate tweak to pull from this table vs the users? Simply setting it to user = AdminUser... did not work. – ylluminate Nov 29 '11 at 06:06
  • Umm, I don't know, query AdminUser model? As for me, I always stored all users in the same tables, with 'roles' attribute assigned. – Sergio Tulentsev Nov 29 '11 at 06:09
  • You can change name of collection that model refers to with :store_in method. So, to look in admin_users table you'd have to add User.store_in 'admin_users' before that code. (this answer implies using of Mongoid) – Sergio Tulentsev Nov 29 '11 at 06:10
  • Attempted `User.store_in 'admin_users'` however received `undefined method`. I'm not seemingly able to access the table as I'm just getting a nil back each time. What about querying the entire table and just getting all entries therein initially to test to see if I'm getting into that table initially? (Working in MySQL here, however that shouldn't matter with ActiveRecord.) – ylluminate Nov 29 '11 at 06:13
  • :store_in is a part of Mongoid gem. You can get access to low(er)-level ruby driver by calling User.db – Sergio Tulentsev Nov 29 '11 at 06:17
  • Okay, thanks. I managed to get that working with some fiddling and exploring of the table first. Interestingly the real problem was not the password, but rather the status of the entry. I was using PostgreSQL initially, but recently switched to MySQL and the "status" attribute was throwing the error! PGSQL stores it as t for true whereas MySQL stores it as 1 (or 0 for false). When copying over the db this evoked a problem and did not convert the t -> 1. LOL, thanks. – ylluminate Nov 29 '11 at 06:31
  • devise is baked in so the use of pw confirmation is redundant. ```User.find_by_email('joe@example.com').update_attributes(:password => 'password')``` – copremesis Jul 21 '17 at 18:22
  • @copremesis: maybe it works _now_, but it certainly didn't work like this 6 years ago :) – Sergio Tulentsev Jul 21 '17 at 19:36
  • 1
    Another few years later in 2020, Rails 6.0 issues a warning *update\_attributes is deprecated and will be removed from Rails 6.1*. Use instead: `user.update(password: new_password)` – Masa Sakano Oct 20 '20 at 12:34
61
# $ rails console production
u=User.where(:email => 'usermail@gmail.com').first
u.password='userpassword'
u.password_confirmation='userpassword'
u.save!
Eric Guo
  • 1,755
  • 17
  • 25
  • 4
    devise is baked in rails so the use of pw confirmation is redundant. ```User.find_by_email('joe@example.com').update_attributes(:password => 'password')``` – copremesis Jul 21 '17 at 18:21
30

If you run the following in the rails console it should do the trick:

User.find_by(email: 'user_email_address').reset_password!('new_password','new_password')

http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Recoverable

gstraehle
  • 611
  • 7
  • 7
  • 7
    Note the exclamation mark is deprecated, it is just : `User.find_by(email: 'user_email_address').reset_password('new_password','new_password')` – IrishDubGuy Nov 03 '16 at 15:01
  • 1
    Also note that you have to enter a *valid password* that confirms to the password requirements in your devise configuration. – zwippie Apr 04 '18 at 09:07
7

You can simply update password field, no need for confirmation password, devise will save it in encrypted form

u = User.find_by_email('user@example.com')
u.update_attribute(:password, '123123')
Kshitij
  • 353
  • 3
  • 10
3

For some reason, (Rails 2.3??)

user = User.where(:email => email).first

didn't work for me, but

user = User.find_by_email('user@example.com')

did it.

valk
  • 9,363
  • 12
  • 59
  • 79
  • The reason for this being that the where(); method was not yet in rails 2.3, we used to use the find(:all, :conditions => conditions) back in the days. – dennis Jan 23 '13 at 09:44
3

1.Login in to ralis console

$ sudo bundle exec rails console production

2.Then update the administrator's password

irb(main):001:0> user = User.where("username = 'root'")
irb(main):002:0> u = user.first
irb(main):003:0> u.password="root2014@Robin"
=> "root2014@Robin"
irb(main):004:0> u.password_confirmation="root2014@Robin"
=> "root2014@Robin"
irb(main):005:0> u.save
=> true
irb(main):006:0> exit

3.Refresh the login page, use the new password to login, enjoy!

Good Luck!

robinwen
  • 852
  • 10
  • 10
  • devise is baked in so the use of pw confirmation is redundant. ```User.find_by_email('joe@example.com').update_attributes(:password => 'password')``` – copremesis Jul 21 '17 at 18:23
2
User.find_by_email('joe@example.com').update_attributes(:password => 'password')
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
copremesis
  • 758
  • 7
  • 7
1

If your account is locked from too many login attempts, you may also need to do:

user.locked_at = ''
user.failed_attempts = '0'
user.save!
Enzio
  • 11
  • 1