Seeding users with Devise in Ruby on Rails

后端 未结 14 2389
终归单人心
终归单人心 2020-12-14 00:59

In my development and test environments, I want to seed the database with a bunch of users. I\'m using Ruby on Rails v3.2.8 and the latest Devise. So I added this line in

相关标签:
14条回答
  • 2020-12-14 01:41

    Don't try create the encrypted password, devise handles that for you. This will work, just make sure the password is a minimum 6 characters long.

    User.create(
            email: "test@test.com",
            password: "123456"
        )
    

    Even better, in your terminal:

    $bundle add faker
    

    Then:

    User.create(
                email: test@test.com,
                password: "123456"
            )
    10.times do
      User.create(
                  email: Faker::Internet.email,
                  password: "123456"
               )
    end
    

    Just make sure you set at least one email in your seeds that you can remember. Hence retaining the 'test' email.

    0 讨论(0)
  • 2020-12-14 01:48

    I Did something same in one of my requirements so just pasting my snippet

    def triggerSeedUsers
          p "Starting Seeding Users..."
          p   "Deleting all users"..
          User.destroy_all
          normal_users = [{:email => 'abc@domain.com', :login => "abc_demo", :name => 'abc Demo'}]
          admin_users = [{:email => 'admin@domain.com', :login => 'abc_admin', :name => 'abc Admin'}]
    
          [normal_users,admin_users].each do |user_type|
            user_type.each do |user|
              User.create!(:name => user[:name],
                :login => user[:login],
                :email => user[:email],
                :first_login => false,
                :password => 'P@ssw0rd',
                :password_confirmation => 'P@ssw0rd'
                )
            end
          end
          User.where('name LIKE ?', '%demo%').update_all(:is_admin => 0)
          User.where('name LIKE ?', '%admin%').update_all(:is_admin => 1)
         end
    
    0 讨论(0)
提交回复
热议问题