No se puede crear una cuenta de Devise usando rake db: seed for Rails 3.0

I'm trying to preload all devise accounts in advance using rake db:seed. Data for all other models seems to be inserted in the database but for some reason NO rows are created for Person model which uses devise. Registration from web interface works fine, but I want to avoid creating accounts manually, thats the reason i'm using rake db:seed. I copied encrypted_password,password_salt from an account created via web interface. Please let me know how to get around this? Many thanks..

people = Person.create(
                        :email => 'nnn@gmail.com',
                        :encrypted_password => '$2a$10$SyacAOhJQtVeTcTPYm.ROuFbhGMylfj4fLrK3NHyeRwfEokKp2NVW',
                        :password_salt => '$2a$10$SyacAOhJQtVeTcTPYm.ROu',
                        :first_name => "nnn",
                        :last_name => "yyy"
                       )


in routes.rb i have.

    devise_for :people

preguntado el 08 de enero de 11 a las 16:01

BTW, this method of seeding users is not very efficient, because for each user Devise checks if there is a user with that email already (a separate query) and the issues a separate insert query - this works really slow for me. I'm currently looking for better solutions. I need to have about 100,000 uses seeded for testing... -

3 Respuestas

I've done this using Devise in the past. I didn't try setting the encrypted password and salt that way. I just set the password and confirmation something like this (I dont have my project handy):

Person.create(:email => 'nnn@gmail.com', :password => 'foobar', :password_confirmation => 'foobar', :first_name => 'nn', :last_name => 'yy')

Trata eso.

Respondido el 08 de enero de 11 a las 20:01

Most likely the "create" method fails quietly due to model validation and therefore returns false. You would've seed the errors if you used "create!" method instead (with exclamation) - this method raises the exception if validation fails.

The likely reason for validation failure in your case is that (by default for Devise) minimum password length is 6 characters and you were not supplying password at all.

respondido 25 mar '11, 17:03

If you don't want duplicates while running rake db:seed multiple times:

User.create(
  email: email,
  password: password,
  password_confirmation: password
) unless User.where(email: email).exists?

Respondido 24 Abr '15, 00:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.