Home>
What you don't understand

I wrote a method to encrypt the password using BCrypt and assign it to the password in User's model file.
After that, I created a seed file and put it in, but when I tried to get the encrypted password, it didn't work.
Rather, I can get it, but I don't understand why I can get it with such a description. I would like somebody to teach.

The column name for User password is
hashed_password
It is described as.

user.rb

class User</pre>
<p><code>seed.rb</code></p>
<pre><code data-language = "Ruby">User.create! (
  password: "hogehoge"
)

And after inputting db: seed, at the terminal
rails r "puts User.first.hashed_password"
Then the encrypted password will be output.

At this time, why should the password of seed.rb be described as password instead of hashed_password?
First in seed.rb
hashed_password: "hogehoge"
Was described. In this case, the output to the terminal will be the hogehoge before it is encrypted.

When creating the table, I used hashed_password for the password column, so I thought I had to use the same hashed_password for seed.rb. What's wrong?

I would be grateful if anyone could tell me. Thank you.

  • Answer # 1

    I think you wrote has_secure_password in model.
    As a result, the process of "encrypting the password before saving and putting it in hashed_password" will be performed.

    I haven't read the code, so it's probably from the behavior.
    I think I'm defining a before_save hook.