userが存在するかどうか判別が可能
> user = User.first
=> #<User id: 1, name: "Michael Hartl", email: "michael@example.com", created_at:, updated_at:, password_digest: >
> if user
> puts "user exist"
> else
> puts "user don't exist"
> end
user exist
=> nil
irb(main):012:0> user = nil
=> nil
> if user
> puts "user exist"
> else
> puts "user don't exist"
> end
user dont exist
=> nil
またuserのパスワードがfoobar
の場合
authenticateメソッド
を使うことでパスワードと一致してるか検証可能
> user = User.first
=> #<User id: 1, name: "Michael Hartl", email: "michael@example.com", created_at:, updated_at:, password_digest: FILTERED]>
> if user.authenticate("foobar")
> puts "Password is correct"
> else
> puts "Password is wrong"
> end
Password is correct
=> nil
> if user.authenticate("")
> puts "Password is correct"
> else
> puts "Password is wrong"
> end
Password is wrong
=> nil