ere*_*ere 3 ruby-on-rails cancan ruby-on-rails-4 rolify cancancan
我有两个模型Organization和Users我正在使用Rolify连接.
用户具有角色,组织是资源.
这很好用但是我的问题是尝试获取特定资源的用户列表.我想获得在该资源上有任何角色的所有用户的列表(我不关心像通用管理员这样的类级别角色等)
我尝试过做类似下面的事情,但它很糟糕,它仍然会返回一个类级管理员列表(那些管理员但实际上并不在任何特定资源上).
class Organization < ActiveRecord::Base
resourcify
def users
# this is terrible and still doesn't work right.
User.with_role(:admin, self) + User.with_role(:press, self)
end
end
class User < ActiveRecord::Base
rolify
def organizations
Organization.with_roles(Role.role_names, self).uniq
end
end
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
试试这个.
has_many :users, through: :roles, class_name: 'User', source: :users
Run Code Online (Sandbox Code Playgroud)
这应该只添加使用角色模型的那些(跳过类中的那些).你也可以尝试更明确的条件,看看这个问题/ PR:
https://github.com/RolifyCommunity/rolify/pull/181
| 归档时间: |
|
| 查看次数: |
827 次 |
| 最近记录: |