Rails的ActiveRecord的:查找以外的所有用户当前用户 [英] Rails ActiveRecord: Find All Users Except Current User
问题描述
我觉得这应该是很简单的,但我的大脑短路就可以了。如果我有重新$ P $对象psenting当前用户,并希望除了当前用户查询所有用户,我怎么能做到这一点,考虑到当前用户有时会零
?
这是我在做什么现在:
高清指数
@users = User.all
@ users.delete CURRENT_USER
结束
我不喜欢的是,我对查询结果进行后期处理。除了感觉有点不对劲,我不认为这将很好地工作,如果我转换查询过要与 will_paginate
运行。任何建议如何使用查询做到这一点?谢谢你。
@users =(current_user.blank User.all:??User.find(:所有,:条件= GT [!?ID =,current_user.id]))
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
?
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.
@users = (current_user.blank? ? User.all : User.find(:all, :conditions => ["id != ?", current_user.id]))
这篇关于Rails的ActiveRecord的:查找以外的所有用户当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!