[英]Rails: Rolify gem not updating the database
So here's is what I did: 所以这就是我所做的:
Select the user: 选择用户:
>> user = User.find(337633)
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 337633], ["LIMIT", 1]]
#<User id: 337633, name: "Restaurant Guy", email: "restaurant@guy.com", mobile: "8000088888", image: "", password_digest: "$2a$10$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>
Check number of roles: 检查角色数量:
>> 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" = $1 [["user_id", 337633]]
1 #customer role
Grant the selected user a role on a specific resource: 授予所选用户特定资源的角色:
>> user.grant :restaurant_admin, Restaurant.first
Restaurant Load (1.0ms) SELECT "restaurants".* FROM "restaurants" ORDER BY "restaurants"."id" ASC LIMIT $1 [["LIMIT", 1]]
Role Load (0.0ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = $1 AND "roles"."resource_type" = $2 AND "roles"."resource_id" = $3 ORDER BY "roles"."id" ASC LIMIT $4 [["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" = $1 [["user_id", 337633]]
Role Load (0.0ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = $1 LIMIT $2 [["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" = $1 AND ("users"."id" != $2) LIMIT $3 [["mobile", "8000088888"], ["id", 337633], ["LIMIT", 1]]
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 AND ("users"."id" != $2) LIMIT $3 [["email", "restaurant@guy.com"], ["id", 337633], ["LIMIT", 1]]
(1.0ms) COMMIT
And save it: 并保存:
>> user.save
(0.0ms) BEGIN
true
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."mobile" = $1 AND ("users"."id" != $2) LIMIT $3 [["mobile", "8000088888"], ["id", 337633], ["LIMIT", 1]]
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 AND ("users"."id" != $2) LIMIT $3 [["email", "restaurant@guy.com"], ["id", 337633], ["LIMIT", 1]]
(1.0ms) COMMIT
Reload from database: 从数据库重新加载:
>> reload!
Reloading...
true
Check if the saved value is present 检查是否存在保存的值
>> user = User.find(337633)
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 337633], ["LIMIT", 1]]
#<User id: 337633, name: "Restaurant Guy", email: "restaurant@guy.com", mobile: "8000088888", image: "", password_digest: "$2a$10$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" = $1 [["user_id", 337633]]
1 #customer role :(
Why wasn't the update persisted? 为什么更新没有持续?
What am I doing wrong? 我究竟做错了什么?
UPDATE : Looks like there's no mistake on saving the updated parts but seems to be a bug in rolify
gem when operated on Rails 5+
更新 :看起来没有保存更新零件的错误 , rolify
在Rails 5+
操作时似乎是rolify
gem的错误
UPDATE 2 : The aforementioned bug was resolved I believe, and I finally found what was causing the ROLLBACK
-> failing to include resourcify . 更新2 :我相信上述错误已解决,我终于找到了导致ROLLBACK
>无法包括resourcify的原因 。
Need to include resourcify
on the resource that should be assigned to the role and the ROLLBACK
was happening as the link was broken. 需要在应该分配给角色的resourcify
上包括重新resourcify
,并且在链接断开时发生了ROLLBACK
。
And I also found that user.save
is useless here as the roles get into the db right after you grant them 而且我还发现user.save
在这里没有用,因为在授予角色后角色就进入了数据库
When you grant the selected user a role on a specific resource, it seems that there's no record add to users_roles
. 当授予所选用户特定资源的角色时,似乎没有记录添加到users_roles
。
>> user.grant :restaurant_admin, Restaurant.first
Restaurant Load (1.0ms) SELECT "restaurants".* FROM "restaurants" ORDER BY "restaurants"."id" ASC LIMIT $1 [["LIMIT", 1]]
Role Load (0.0ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = $1 AND "roles"."resource_type" = $2 AND "roles"."resource_id" = $3 ORDER BY "roles"."id" ASC LIMIT $4 [["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" = $1 [["user_id", 337633]]
Role Load (0.0ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = $1 LIMIT $2 [["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" = $1 AND ("users"."id" != $2) LIMIT $3 [["mobile", "8000088888"], ["id", 337633], ["LIMIT", 1]]
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 AND ("users"."id" != $2) LIMIT $3 [["email", "restaurant@guy.com"], ["id", 337633], ["LIMIT", 1]]
(1.0ms) COMMIT
it was ROLLBACKed. 它已回滚。 so i think there's something wrong with the method user#grant
所以我认为方法user#grant
出了点问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.