Search code examples
ruby-on-railsclearance

Is it possible to seed users into the DB with the Clearance gem?


I'm working on a Ruby-on-Rails project using the Clearance gem for authentication. Is there a way to use the seeds.rb file to seed users into my database, or do I have to manually create users on the sign-in page? It looks like the user table is structured with an encrypted-password attribute so I'm not sure how to go about seeding.


Solution

  • The documentation does not say you have to do anything fancy.

    def create_user(email)
      User.create!(
        email:  email,
        password: "password1234"
      )
    end
    
    #fake  users
    1...50.times do |i|
      create_user("person_#{i}@test#{i}.io")
    end
    

    in console

      User.last
      => <User id: 50, created_at: "2018-05-31 18:51:57", updated_at: "2018-05-31 18:51:57", email: "[email protected]", encrypted_password: "$2a$10$JBWpdFocyd1TTBy5W30uYuWaD5nTo1TPErtQpt2nNZO...", confirmation_token: nil, remember_token: "cfaf107ea30d3303bb991f61973368b2a1fd44c7">