資料庫內雖然儲存密碼的欄位是“password_digest”,但是實際要把它的欄位名稱當成“password”來使用。
2.1.2 :001 > user = User.find_by username: "BBB"
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."username" = 'BBB' LIMIT 1
=> #
2.1.2 :002 > exit
kuruwateki-MacBook-Air:postit-solution shawnkuo67$ rails c
Loading development environment (Rails 4.0.0)
2.1.2 :001 > user = User.find_by username: "BBB"
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."username" = 'BBB' LIMIT 1
=> #
2.1.2 :002 > user.password= 'abcdef'
=> "abcdef"
2.1.2 :003 > user.save
(0.2ms) begin transaction
Binary data inserted for `string` type on column `password_digest`
SQL (6.0ms) UPDATE "users" SET "password_digest" = ?, "updated_at" = ? WHERE "users"."id" = 2 [["password_digest", "$2a$10$zo5T8V6MYPx3Ri8Ewah9aejNTI7grpEj9tTeVSmUoD.QyheSRhBsS"], ["updated_at", Mon, 07 May 2018 06:40:48 UTC +00:00]]
(1.0ms) commit transaction
=> true
2.1.2 :004 > user = User.find_by username: "BBB"
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."username" = 'BBB' LIMIT 1
=> #
2.1.2 :005 > user.authenticate("ddd")
=> false
2.1.2 :006 > user.authenticate("abcdef")
=> #
留言
張貼留言