Rails 模型找到不相等的地方 [英] Rails Model find where not equal

查看:36
本文介绍了Rails 模型找到不相等的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不相等的条件下在我的数据库中查找记录?我现在有了这个,但是有没有一种用 Rails 说的花哨的方法?

How can I find records in my database on a not equal condition? I have this now, but is there a fancy rails-speak way of doing it?

GroupUser.where('user_id != ?',me)

推荐答案

在 Rails 4.x 中(参见 http://edgeguides.rubyonrails.org/active_record_querying.html#not-conditions)

In Rails 4.x (See http://edgeguides.rubyonrails.org/active_record_querying.html#not-conditions)

GroupUser.where.not(user_id: me)

在 Rails 3.x 中

In Rails 3.x

GroupUser.where(GroupUser.arel_table[:user_id].not_eq(me))

要缩短长度,您可以将 GroupUser.arel_table 存储在变量中,或者如果在模型内部使用 GroupUser 本身,例如,在 scope 中>,您可以使用 arel_table[:user_id] 而不是 GroupUser.arel_table[:user_id]

To shorten the length, you could store GroupUser.arel_table in a variable or if using inside the model GroupUser itself e.g., in a scope, you can use arel_table[:user_id] instead of GroupUser.arel_table[:user_id]

Rails 4.0 语法归功于 @jbearden 的回答

这篇关于Rails 模型找到不相等的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆