I am using Devise for authenticating my app users. (In my app the authenticatable model is the Contact model, not the User model).
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", "QualityContact@fake.com"], ["Coordination", "CoordinationContact@fake.com"], ["Quality manager", "QualityManagerContact@fake.com"], ["Client services", "ClientServiceContact@fake.com"], ["Client services manager", "ClientServicesManager@fake.com"], ["Accounting", "Accounting@fake.com"], ["General manager", "GeneralManager@fake.com"] ] #Create roles role_list.each do |role, email| Role.find_or_create_by!(name: role) end #Create contacts !!!-take out before deploying to production-!!! role_list.each do |role, email| Contact.find_or_create_by!(email: email, password: "password") end
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") end