我在Hartl教程结束时使用state_machine gem来设置注册电子邮件确认过程(我知道这对于这个简单的用例来说代码很重,但我做到了这一点学)。
我已将以下代码插入到我的用户模型中:
state_machine initial: :inactive do
event :confirm do
transition :inactive => :active
end
end
当我进入rails控制台时,找到一个用户并尝试从非活动转换为活动它失败:
2.0.0-p481 :040 > u = User.find(1)
User Load (2.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
=> #<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2014-09-09 08:28:20", updated_at: "2014-09-09 08:28:20", password_digest: "$2a$10$.Fhol/ebeFxVKT1HTvDTce/c.zC5D0MLMH.fTqPzMnNf...", remember_token: "bdfa52f05e2f8357ff95e6354af0565a6dca9a83", admin: true, state: "inactive">
2.0.0-p481 :041 > u.state
=> "inactive"
2.0.0-p481 :042 > u.state_events
=> [:confirm]
2.0.0-p481 :043 > u.confirm
(0.3ms) BEGIN
User Exists (4.8ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('example@railstutorial.org') AND "users"."id" != 1) LIMIT 1
(0.9ms) ROLLBACK
=> false
我出错的任何想法?我怀疑它与此片段有关:
"users"."id" != 1
但我不知道sql查询为什么会这样做?