I have a simple Rails 4 app with only two empty models, User and Job. And I'm using the Rolify gem, in strict mode, to give the Users roles.
i.e (Strict Mode per the documentation)
class User < ActiveRecord::Base
rolify strict: true
end
Essentially I want Users with the role engineer to be able to have certain permissions for the Job class, but not necessarily permissions for a specific Job.
So I give user1 the role of engineer for the Job class and for an instance of the Job class. While user2 only gets the role of engineer for the Job class. Just like the documentation.
job = Job.create() # Job class instance
user1.add_role(:engineer, Job) # Job class
user1.add_role(:engineer, job) # Job class instance
user2.add_role(:engineer, Job) # Job class
Which works fine and I can say things like;
user1.has_role? :engineer, Job #=> true
user1.has_role? :engineer, job #=> true
user2.has_role? :engineer, Job #=> true
user2.has_role? :engineer, job #=> false This is the result I expect.
But when I try to make a class level rolify call
(i.e Class Level per the documentation)
User.with_role(:engineer, job)
# => [ list of User instances that have a scoped role of "engineer" to a job instance ]
It returns both user1 and user2. When I only expected user1 because it was specifically tied to that instance. Does strict mode not work that way or am I doing something wrong?
答案 0 :(得分:0)
我认为这是一个错误,所以我在这里提出了一个问题:https://github.com/RolifyCommunity/rolify/issues/362
为了继续您的生活,您可以对返回的每个用户进行has_role?
重复检查。
User.with_role(:engineer, job).select{|user| user.has_role?(:engineer, job)}