Rails 3 和 Acl9:查找给定对象/角色的所有主题
我使用 rails3 和 acl9 进行授权。我正在尝试理解 ARel 和 Rails 的查询方式。
我有一个属于公司的用户,并且该用户在该公司中具有给定的角色(acl9 提供管道)。如何从给定的公司获取具有特定角色的所有用户? 我想过滤数据库中的结果,而不是在应用程序中过滤它们。
我想要这样的东西:
#
# 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 api,但我来自Java/Grails地方,并且我使用的大多数ORM都有一些ORM api(我想datamapper是这样工作的,但我使用的是普通的rails3 )。
I'm using rails3 with acl9 for authorization. I'm trying to grok ARel and the rails way of querying.
I have a User that belongs to a Company, and the user has a given role (acl9 provides the plumbing) over the Company. How can I get all Users from a given Company that have a specific role? I want to filter the results in the DB, not filter them in the application.
I want something like this:
#
# 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)
I know rails3 has a sql api, but I come from a Java/Grails place, and most of the ORMs I use have some ORM api (I suppose datamapper works like this, but I'm using plain rails3).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现:
I found out:
我还在 acl9 1.2 中添加了此功能,所以现在您可以执行以下操作:
I also added this feature in acl9 1.2, so now you can just do: