我使用rails3和acl9进行授权。我正在尝试ARel和rails查询的方式。
我有一个属于公司的用户,用户在公司上有一个给定的角色(acl9提供管道)。如何才能让具有特定角色的给定公司的所有用户?--我想在DB中过滤结果,而不是在应用程序中过滤它们。
我想要这样的东西:
#
# authorizable_id = 1 is the company's id
# 'collaborator' and '1' would be params for my scope
#
select * from users
inner join roles_users
on roles_users.user_id = users.id
inner join roles
on roles_users.role_id = roles.id
where roles.authorizable_type='Company'
and roles.authorizable_id = 1
and roles.name = 'collaborator';
#usage with scope
@collaborators = User.with_role_and_company('collaborator',current_user.company)我知道rails3有一个sql,但是我来自一个Java/Grails地方,我使用的大多数ORMs都有一些ORMs(我想datamapper是这样工作的,但我使用的是普通的rails3)。
发布于 2010-11-23 00:14:57
我发现:
class << self
def with_role_in_company(role, company)
joins(:roles).
where(:roles => {:authorizable_type => 'Company'}).
where(:roles => {:authorizable_id => company.id}).
where(:roles => {:name => role})
end
end发布于 2015-01-22 21:18:51
我还在acl9 1.2中添加了这个特性,所以现在您可以这样做了:
company.users :collaboratorhttps://stackoverflow.com/questions/4240468
复制相似问题