Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Fixtures with BCrypt

I'm having a problem with fixtures for BCrypt password: my User model is both setup with has_secure_password and validates_presence_of :password.

The point is that BCrypt uses password and password_confirmation but in the schema there is only the password_digest field.

The fixture is complaining that the password field does not exists.

How can I avoid this?

Thank you

like image 924
Bruno Canongia Avatar asked Jun 17 '15 14:06

Bruno Canongia


1 Answers

Apparently for performance reasons fixtures are being pushed to the database directly. That means that instead of password: you need password_digest: in your fixtures:

test_user:
  email: "[email protected]"
  password_digest: <%= BCrypt::Password.create('testpassword', cost: 5) %>

when using bcrypt based passwords with has_secure_password. As mentioned in the comments cost argument is optional. If you don't use it a sensible default will be used.

Update: in Rails 6 bcrypt is not available by default and needs to be explicitly added/required

like image 106
silverdr Avatar answered Oct 12 '22 04:10

silverdr