Search code examples
ruby-on-railsdevise

Generate devise password manually and save record along with other attributes?


I'm trying to apply the technique here to manually generate a password with devise and save it along with other attributes.

I'm using

password = "iamben"
@user = User.new(first_name: "Ben", email: "ben@mail.com", encrypted_password: password).encrypted_password
@user.save

NoMethodError (undefined method `save' for "iamben":String)

I am not sure why it doesn't work. I also tried a couple of other attempts

@user = User.new(first_name: "Ben", email: "ben@mail.com", encrypted_password: password.encrypted_password)

and

@user = User.new(first_name: "Ben", email: "ben@mail.com", encrypted_password: encrypted_password(password))

Sorry, it's probably something very simple I am doing incorrectly


Solution

  • I had a few points of confusion, but the answer essentially boils down to what we find here.

    Simply

    1. find the user record whose password you want to change (e.g. user = User.where(email: "ben@mail.com")
    2. user.password = "123abc"
    3. user.save
    4. That's it! That user's password is now 123abc