我觉得这应该是很简单的,但我的大脑短路就可以了。如果我有重新$ 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 endWhat 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的:查找以外的所有用户当前用户
发布评论