简体   繁体   English

Rails ActiveRecord:如何选择具有给定权限的所有用户?

[英]Rails ActiveRecord: How to select all users with a given permission?

I've got a model called Users, some of whom are considered Authors. 我有一个名为“用户”的模型,其中一些被认为是“作者”。 This is accomplished through a table called Roles, which lists a number of different roles: "Author" is one. 这可以通过称为角色的表来完成,该表列出了许多不同的角色:“作者”就是其中之一。 A many-to-many association in a table called Permissions links Roles and Users. 名为“权限”的表中的多对多关联将角色和用户链接在一起。

To check whether a User has the Author permission, I have a method that runs through the Permissions linked to that particular User and returns whether any of them also links to the Author permission. 为了检查用户是否具有“作者”权限,我有一个方法,该方法通过链接到该特定用户的“权限”运行,并返回其中的任何一个是否也链接到“作者”权限。

This system has served me well. 这个系统对我很好。 However, it makes it clunky to search and order the Authors on the site. 但是,它使在网站上搜索和排序作者变得笨拙。 What I'd like to do is something simple and graceful, ideally like a named scope that will allow me to say things like Users.authors.order("date_joined") or something like that. 我想做的事情是简单而优雅的,理想情况下是一个命名范围,它使我可以说诸如Users.authors.order(“ date_joined”)之类的东西。

As it is right now, I don't have any way to get the group of "Author" users other than pulling all Users out of the database, running through them one at a time, searching for their Author permission, and then adding them to an array of Users if it is found. 目前,除了将所有用户从数据库中拉出,一次运行一次,搜索其“作者”权限,然后添加它们之外,我没有任何其他方法可以让“ Author”用户组找到一个用户数组。

This seems rather ungraceful, especially as I have to do it every time I want to present the list of Authors in a view. 这似乎很不舒服,尤其是每次我要在视图中显示“作者列表”时都必须这样做。

Is there a better way? 有没有更好的办法?

EDIT -- Solution: 编辑-解决方案:

Following the helpful advice below and the tips from http://railscasts.com/episodes/215-advanced-queries-in-rails-3?view=asciicast I was able to put together the following. 遵循以下有用的建议以及http://railscasts.com/episodes/215-advanced-queries-in-rails-3?view=asciicast中的提示,我可以将以下内容汇总在一起。

In User.rb: 在User.rb中:

    class User < ActiveRecord::Base
      scope :authors, joins(:permissions) & Permission.author_permissions
      scope :admins, joins(:permissions) & Permission.admin_permissions

In Permission.rb: 在Permission.rb中:

    class Permission < ActiveRecord::Base
      scope :author_permissions, where("role_id = ?", Role.find_by_rolename("author"))
      scope :admin_permissions, where("role_id = ?", Role.find_by_rolename("administrator"))

And voila! 瞧! Now I can call Users.authors and it works like a charm. 现在,我可以调用Users.authors ,它就像一个Users.authors

Keep the schema structure that you have but make sure to add the proper table indexes. 保留您拥有的架构结构,但确保添加适当的表索引。

Then to query for users in specific roles you can do 然后,您可以查询具有特定角色的用户

User.all(:joins => :roles,
         :conditions => {:roles => {:id => pick_a_role_id}})

have you looked at CanCan ? 你看过CanCan吗? It will probably require a little refactoring, mostly to create a current_user method. 可能需要进行一些重构,主要是为了创建一个current_user方法。 A guide to roles can be found here 角色指南可在此处找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM