Devise and seeds.rb: password vs. encrypted_password

I am using Devise for authenticating my app users. (In my app the authenticatable model is the Contact model, not the User model).

My seeds.rb file was supposed create several different contacts for me to experiment with on the development and staging servers. It was supposed to use the find_or_create_by! method so that duplicate contacts would not be created, even if I ran rake db:seed more than once. However, it was not finding the duplicate records, and it was trying to create new records even though those contacts already existed in the database. Here is an excerpt from the original code:

role_list = [
  ["Quality",                 ""],
  ["Coordination",            ""],
  ["Quality manager",         ""],
  ["Client services",         ""],
  ["Client services manager", ""],
  ["Accounting",              ""],
  ["General manager",         ""]

#Create roles
role_list.each do |role, email|
  Role.find_or_create_by!(name: role)

#Create contacts !!!-take out before deploying to production-!!!
role_list.each do |role, email|
  Contact.find_or_create_by!(email: email, password: "password")

For the record, those email / password pairs are completely fake and the app can’t be accessed using them.

The problem was that Devise stores the password in a column called encrypted_password. There is no such column in the database as password, so of course find_or_create! could not find the existing records!

I fixed the problem by updating the relevant portion of seeds.rb file as such:

#Create contacts --!! take out before deploying to production.  You can log in with these contacts using the password: "password".
role_list.each do |role, email|
  Contact.find_or_create_by!(email: email,
                            encrypted_password: "$2a$10$cHyiV6SD/vdO674gzqZCP.mbSMjcct.gU..aS2Vi7nA.DZ8bILjZy")

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s