Create a devise user from Ruby console

Ruby on-RailsRubyDevise

Ruby on-Rails Problem Overview


Any idea on how to create and save a new User object with devise from the ruby console?

When I tried to save it, I'm getting always false. I guess I'm missing something but I'm unable to find any related info.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You can add false to the save method to skip the validations if you want.

User.new({:email => "[email protected]", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" }).save(false)

Otherwise I'd do this

User.create!({:email => "[email protected]", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" })

If you have confirmable module enabled for devise, make sure you are setting the confirmed_at value to something like Time.now while creating.

Solution 2 - Ruby on-Rails

You should be able to do this using

u = User.new(:email => "[email protected]", :password => 'password', :password_confirmation => 'password')
u.save

if this returns false, you can call

u.errors

to see what's gone wrong.

Solution 3 - Ruby on-Rails

When on your model has :confirmable option this mean the object user should be confirm first. You can do two ways to save user.

a. first is skip confirmation:

newuser = User.new({email: '[email protected]', password: 'password', password_confirmation: 'password'})
newuser.skip_confirmation!
newuser.save

b. or use confirm! :

newuser = User.new({email: '[email protected]', password: 'password', password_confirmation: 'password'})
newuser.confirm!
newuser.save

Solution 4 - Ruby on-Rails

If you want to avoid sending confirmation emails, the best choice is:

    u = User.new({
      email: '[email protected]',
      password: '12feijaocomarroz',
      password_confirmation: '12feijaocomarroz'
    })

    u.confirm
    u.save

So if you're using a fake email or have no internet connection, that'll avoid errors.

Solution 5 - Ruby on-Rails

None of the above answers worked for me.

This is what I did:

User.create(email: "[email protected]", password: "asdasd", password_confirmation: "asdasd")

Keep in mind that the password must be bigger than 6 characters.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartinView Question on Stackoverflow
Solution 1 - Ruby on-RailsjspoonerView Answer on Stackoverflow
Solution 2 - Ruby on-RailsSam RitchieView Answer on Stackoverflow
Solution 3 - Ruby on-RailsakbarbinView Answer on Stackoverflow
Solution 4 - Ruby on-RailsFlavio WuenscheView Answer on Stackoverflow
Solution 5 - Ruby on-RailsEzequiel RamiroView Answer on Stackoverflow