ruby on rails - How does rolify keep track of roles? -
rolify has join table called user_roles keeps track of relationships:
user_id role_id
but here's what's weird. when empty table of data each user still has role remembered.
i'm trying manipulate database in specs, , it's impossible because of spookiness.
look @ console:
user = user.save irb(main):022:0> user.roles # nothing weird, have before filter make every new user guest => #<activerecord::associations::collectionproxy [#<role id: 1, name: "guest", resource_id: nil, resource_type: nil, created_at: "2014-08-17 10:04:57", updated_at: "2014-08-17 10:04:57">]> irb(main):023:0> usersrole.all.each |role| puts role.inspect end => [#<usersrole user_id: 2, role_id: 1, id: 4>, #<usersrole user_id: 2, role_id: 1, id: 5>] irb(main):024:0> usersrole.all.each |role| role.delete end => [] # empty! irb(main):026:0> user.roles # no way should => #<activerecord::associations::collectionproxy [#<role id: 1, name: "guest", resource_id: nil, resource_type: nil, created_at: "2014-08-17 10:04:57", updated_at: "2014-08-17 10:04:57">]>
i'd appreciate why happening.
you should reload object after deleting roles. rails caches association.
user = user.save irb(main):022:0> user.roles => #<activerecord::associations::collectionproxy [#<role id: 1, name: "guest", resource_id: nil, resource_type: nil, created_at: "2014-08-17 10:04:57", updated_at: "2014-08-17 10:04:57">]> irb(main):023:0> usersrole.all.each |role| puts role.inspect end => [#<usersrole user_id: 2, role_id: 1, id: 4>, #<usersrole user_id: 2, role_id: 1, id: 5>] irb(main):024:0> usersrole.all.each |role| role.delete end => [] # empty! irb(main):026:0> user.reload.roles
Comments
Post a Comment