ruby-on-railsrails-consolesavechangesrolifyupdate-attributes

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+


UPDATE 2: The aforementioned bug was resolved I believe, and I finally found what was causing the ROLLBACK -> failing to include resourcify.


Solution

  • Need to include resourcify on the resource that should be assigned to the role and the ROLLBACK was happening as the link was broken.

    And I also found that user.save is useless here as the roles get into the db right after you grant them