Ruby-on-rails Rails ActiveRecord:查找除当前用户之外的所有用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2672744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails ActiveRecord: Find All Users Except Current User
提问by SingleShot
I feel this should be very simple but my brain is short-circuiting on it. If I have an object representing the current user, and want to query for all users except the current user, how can I do this, taking into account that the current user can sometimes be nil?
我觉得这应该很简单,但我的大脑正在短路。如果我有一个代表当前用户的对象,并且想查询除当前用户之外的所有用户,我该怎么做,考虑到当前用户有时可能是nil?
This is what I am doing right now:
这就是我现在正在做的事情:
def index
@users = User.all
@users.delete current_user
end
What I don't like is that I am doing post-processing on the query result. Besides feeling a little wrong, I don't think this will work nicely if I convert the query over to be run with will_paginate. Any suggestions for how to do this with a query? Thanks.
我不喜欢的是我正在对查询结果进行后处理。除了感觉有点不对之外,我认为如果我将查询转换为使用will_paginate. 有关如何通过查询执行此操作的任何建议?谢谢。
回答by Mohamad
It is possible to do the following in Rails 4 and up:
在 Rails 4 及更高版本中可以执行以下操作:
User.where.not(id: id)
You can wrap it in a nice scope.
你可以把它包装在一个很好的范围内。
scope :all_except, ->(user) { where.not(id: user) }
@users = User.all_except(current_user)
Or use a class method if you prefer:
或者,如果您愿意,可以使用类方法:
def self.all_except(user)
where.not(id: user)
end
Both methods will return an AR relation object. This means you can chain method calls:
这两种方法都将返回一个 AR 关系对象。这意味着您可以链接方法调用:
@users = User.all_except(current_user).paginate
You can exclude any number of users because where()also accepts an array.
您可以排除任意数量的用户,因为它where()也接受一个数组。
@users = User.all_except([1,2,3])
For example:
例如:
@users = User.all_except(User.unverified)
And even through other associations:
甚至通过其他协会:
class Post < ActiveRecord::Base
has_many :comments
has_many :commenters, -> { uniq }, through: :comments
end
@commenters = @post.commenters.all_except(@post.author)
See where.not()in the API Docs.
见where.not()在API文档。
回答by jdl
@users = (current_user.blank? ? User.all : User.find(:all, :conditions => ["id != ?", current_user.id]))
回答by Jakub Kosiński
You can also create named_scope, e.g. in your model:
您还可以创建named_scope,例如在您的模型中:
named_scope :without_user, lambda{|user| user ? {:conditions => ["id != ?", user.id]} : {} }
and in controller:
并在控制器中:
def index
@users = User.without_user(current_user).paginate
end
This scope will return all users when called with nil and all users except given in param in other case. The advantage of this solution is that you are free to chain this call with other named scopes or will_paginate paginate method.
此范围将在使用 nil 调用时返回所有用户,以及在其他情况下在 param 中给出的所有用户除外。此解决方案的优点是您可以自由地将此调用与其他命名范围或 will_paginate 分页方法链接起来。
回答by Harish Shetty
Here is a shorter version:
这是一个较短的版本:
User.all :conditions => (current_user ? ["id != ?", current_user.id] : [])
回答by Steve Bourne
One note on GhandaL's answer - at least in Rails 3, it's worth modifying to
关于 GhandaL 答案的一个注释——至少在 Rails 3 中,值得修改为
scope :without_user, lambda{|user| user ? {:conditions => ["users.id != ?", user.id]} : {} }
(the primary change here is from 'id != ...' to 'users.id !=...'; also scope instead of named_scope for Rails 3)
(这里的主要变化是从 'id != ...' 到 'users.id !=...';对于 Rails 3,也是范围而不是 named_scope)
The original version works fine when simply scoping the Users table. When applying the scope to an association (e.g. team.members.without_user(current_user).... ), this change was required to clarify which table we're using for the id comparison. I saw a SQL error (using SQLite) without it.
当简单地限定用户表时,原始版本工作正常。将范围应用于关联时(例如 team.members.without_user(current_user).... ),需要进行此更改以阐明我们使用哪个表进行 id 比较。我看到一个没有它的 SQL 错误(使用 SQLite)。
Apologies for the separate answer...i don't yet have the reputation to comment directly on GhandaL's answer.
为单独的答案道歉......我还没有直接评论 GhandaL 答案的声誉。
回答by Phil Duffney
Very easy solution I used
我使用的非常简单的解决方案
@users = User.all.where("id != ?", current_user.id)
回答by user3202320
User.all.where("id NOT IN(?)", current_user.id) will through exception
undefined method where for #<Array:0x0000000aef08f8>
User.all.where("id NOT IN(?)", current_user.id) 将通过异常 undefined 方法 where for #<Array:0x0000000aef08f8>
User.where("id NOT IN (?)", current_user.id)
回答by Asif Alam
an array would be more helpful
数组会更有帮助
arrayID[0]=1
数组ID[0]=1
arrayID[1]=3
数组ID[1]=3
User.where.not(id: arrayID)
User.where.not(id: arrayID)
回答by Lana Kushnir
User.where(:id.ne=> current_user.id)
User.where(:id.ne=> current_user.id)
回答by CeeJey Official
Another easy way you could do it:
另一种简单的方法可以做到:
@users = User.all.where("id NOT IN(?)", current_user.id)

