154

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.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Martin
  • 11,216
  • 23
  • 83
  • 140
  • 1
    Not an answer to your question, and you probably already know about Railcasts, but I found these video's useful when learning about Devise: http://railscasts.com/episodes/209-introducing-devise, http://railscasts.com/episodes/210-customizing-devise. They have a few more really useful videos about Devise on there too. Good luck. –  Nov 30 '10 at 19:08
  • 2
    Yea i watched them both, but they don't say anything about what i'm asking. – Martin Dec 06 '10 at 14:40

5 Answers5

205

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

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

Otherwise I'd do this

User.create!({:email => "guy@gmail.com", :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.

Raj
  • 22,346
  • 14
  • 99
  • 142
jspooner
  • 10,975
  • 11
  • 58
  • 81
  • 51
    Looks like save(false) is depcreated, now should be save(:validate => false) – Martin Dec 06 '10 at 14:47
  • 9
    there is just so much magic happening here.. The User model extends Active Record. how come the create method is overriden. Where is the password being encrypted? – codeAnand Dec 16 '11 at 11:15
  • 2
    What does the devise call actually do – codeAnand Dec 16 '11 at 11:19
  • Seems like a wrong way of doing. This way of forcibly validating (or skipping validation) won't set the encrypted_password, hence making this record useless in the real app. – VPaul Jan 24 '18 at 15:51
80

You should be able to do this using

u = User.new(:email => "user@name.com", :password => 'password', :password_confirmation => 'password')
u.save

if this returns false, you can call

u.errors

to see what's gone wrong.

Sam Ritchie
  • 10,988
  • 4
  • 42
  • 53
30

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: 'superadmin1@testing.com', password: 'password', password_confirmation: 'password'})
newuser.skip_confirmation!
newuser.save

b. or use confirm! :

newuser = User.new({email: 'superadmin2@testing.com', password: 'password', password_confirmation: 'password'})
newuser.confirm!
newuser.save
akbarbin
  • 4,985
  • 1
  • 28
  • 31
7

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

    u = User.new({
      email: 'demo@greenant.com.br',
      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.

Flavio Wuensche
  • 9,460
  • 1
  • 57
  • 54
3

None of the above answers worked for me.

This is what I did:

User.create(email: "a@a.com", password: "asdasd", password_confirmation: "asdasd")

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

Ezequiel Ramiro
  • 760
  • 1
  • 12
  • 29