Rails: Rolify gem 未更新数据库

Rails: Rolify gem not updating the database

这就是我所做的:

Select 用户:

>> user = User.find(337633)
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" =  LIMIT   [["id", 337633], ["LIMIT", 1]]
#<User id: 337633, name: "Restaurant Guy", email: "restaurant@guy.com", mobile: "8000088888", image: "", password_digest: "a$H3TlQT1DdGOPQjSR7b1st.SVvAg5XiFidrfqzyqz0RW...", created_at: "2017-02-23 18:19:29", updated_at: "2017-02-23 18:21:43", uid: nil, provider: nil, verified_at: nil, location_id: nil>

检查角色数量:

>> user.roles.length
  Role Load (1.0ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" =   [["user_id", 337633]]
1   #customer role

授予所选用户对特定资源的角色:

>> user.grant :restaurant_admin, Restaurant.first
  Restaurant Load (1.0ms)  SELECT  "restaurants".* FROM "restaurants" ORDER BY "restaurants"."id" ASC LIMIT   [["LIMIT", 1]]
  Role Load (0.0ms)  SELECT  "roles".* FROM "roles" WHERE "roles"."name" =  AND "roles"."resource_type" =  AND "roles"."resource_id" =  ORDER BY "roles"."id" ASC LIMIT   [["name", "restaurant_admin"], ["resource_type", "Restaurant"], ["resource_id", 1], ["LIMIT", 1]]
   (1.0ms)  BEGIN
   (0.0ms)  ROLLBACK
  HABTM_Roles Load (0.0ms)  SELECT "users_roles".* FROM "users_roles" WHERE "users_roles"."user_id" =   [["user_id", 337633]]
  Role Load (0.0ms)  SELECT  "roles".* FROM "roles" WHERE "roles"."id" =  LIMIT   [["id", 4], ["LIMIT", 1]]
#<Role id: nil, name: "restaurant_admin", resource_type: "Restaurant", resource_id: 1, created_at: nil, updated_at: nil>
  Role Load (0.0ms)  SELECT "roles".* FROM "roles" WHERE "roles"."id" = 4
>> user.save
   (0.0ms)  BEGIN
true
  User Exists (1.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."mobile" =  AND ("users"."id" != ) LIMIT   [["mobile", "8000088888"], ["id", 337633], ["LIMIT", 1]]
  User Exists (1.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" =  AND ("users"."id" != ) LIMIT   [["email", "restaurant@guy.com"], ["id", 337633], ["LIMIT", 1]]
   (1.0ms)  COMMIT

并保存:

>> user.save
   (0.0ms)  BEGIN
true
  User Exists (1.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."mobile" =  AND ("users"."id" != ) LIMIT   [["mobile", "8000088888"], ["id", 337633], ["LIMIT", 1]]
  User Exists (1.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" =  AND ("users"."id" != ) LIMIT   [["email", "restaurant@guy.com"], ["id", 337633], ["LIMIT", 1]]
   (1.0ms)  COMMIT

从数据库重新加载:

>> reload!
Reloading...
true

检查保存的值是否存在

>> user = User.find(337633)
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" =  LIMIT   [["id", 337633], ["LIMIT", 1]]
#<User id: 337633, name: "Restaurant Guy", email: "restaurant@guy.com", mobile: "8000088888", image: "", password_digest: "a$H3TlQT1DdGOPQjSR7b1st.SVvAg5XiFidrfqzyqz0RW...", created_at: "2017-02-23 18:19:29", updated_at: "2017-02-23 18:21:43", uid: nil, provider: nil, verified_at: nil, location_id: nil>
>> user.roles.length
  Role Load (0.0ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" =   [["user_id", 337633]]
1  #customer role :(

为什么更新没有持久化?

我做错了什么?


UPDATE: 保存更新的部分貌似没有错误但是操作的时候好像是bug in rolify gem在 Rails 5+


更新 2:我相信上述错误已解决,我终于找到了导致 ROLLBACK -> include resourcify 失败的原因。

基于此SO answer尝试直接在用户对象上调用重新加载:

user.reload

似乎 rails 控制台在被引用后不会重新加载 类。

当您授予所选用户对特定资源的角色时,似乎没有记录添加到 users_roles

>> user.grant :restaurant_admin, Restaurant.first
  Restaurant Load (1.0ms)  SELECT  "restaurants".* FROM "restaurants" ORDER BY "restaurants"."id" ASC LIMIT   [["LIMIT", 1]]
  Role Load (0.0ms)  SELECT  "roles".* FROM "roles" WHERE "roles"."name" =  AND "roles"."resource_type" =  AND "roles"."resource_id" =  ORDER BY "roles"."id" ASC LIMIT   [["name", "restaurant_admin"], ["resource_type", "Restaurant"], ["resource_id", 1], ["LIMIT", 1]]
   (1.0ms)  BEGIN
   (0.0ms)  ROLLBACK
  HABTM_Roles Load (0.0ms)  SELECT "users_roles".* FROM "users_roles" WHERE "users_roles"."user_id" =   [["user_id", 337633]]
  Role Load (0.0ms)  SELECT  "roles".* FROM "roles" WHERE "roles"."id" =  LIMIT   [["id", 4], ["LIMIT", 1]]
#<Role id: nil, name: "restaurant_admin", resource_type: "Restaurant", resource_id: 1, created_at: nil, updated_at: nil>
  Role Load (0.0ms)  SELECT "roles".* FROM "roles" WHERE "roles"."id" = 4
>> user.save
   (0.0ms)  BEGIN
true
  User Exists (1.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."mobile" =  AND ("users"."id" != ) LIMIT   [["mobile", "8000088888"], ["id", 337633], ["LIMIT", 1]]
  User Exists (1.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" =  AND ("users"."id" != ) LIMIT   [["email", "restaurant@guy.com"], ["id", 337633], ["LIMIT", 1]]
   (1.0ms)  COMMIT

已回滚。 所以我认为这个方法有问题 user#grant

需要在应分配给该角色的资源上包含 resourcify,并且 ROLLBACK 正在发生,因为 link 已损坏。

而且我还发现 user.save 在这里没用 因为角色在您授予它们后立即进入数据库