Rails的ActiveRecord的:查找以外的所有用户当前用户

编程入门 行业动态 更新时间:2024-10-25 04:16:12
本文介绍了Rails的ActiveRecord的:查找以外的所有用户当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

本文发布于:2023-10-24 17:22:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524545.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   Rails   ActiveRecord

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!