Ruby-on-rails Rails 模型找到不相等的地方
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5426421/
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 Model find where not equal
提问by Colin
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?
如何在不相等的条件下在我的数据库中查找记录?我现在有了这个,但是有没有一种花哨的 Rails-speak 方式来做到这一点?
GroupUser.where('user_id != ?',me)
回答by Vikrant Chaudhary
In Rails 4.x (See http://edgeguides.rubyonrails.org/active_record_querying.html#not-conditions)
在 Rails 4.x 中(参见http://edgeguides.rubyonrails.org/active_record_querying.html#not-conditions)
GroupUser.where.not(user_id: me)
In Rails 3.x
在 Rails 3.x 中
GroupUser.where(GroupUser.arel_table[:user_id].not_eq(me))
To shorten the length, you could store GroupUser.arel_tablein a variable or if using inside the model GroupUseritself e.g., in a scope, you can use arel_table[:user_id]instead of GroupUser.arel_table[:user_id]
为了缩短长度,您可以存储GroupUser.arel_table在一个变量中,或者如果在模型GroupUser本身内部使用,例如,在 a 中scope,您可以使用arel_table[:user_id]而不是GroupUser.arel_table[:user_id]
Rails 4.0 syntax credit to @jbearden's answer
Rails 4.0 语法归功于@jbearden 的回答
回答by jbearden
Rails 4
导轨 4
GroupUser.where.not(user_id: me)
回答by Jakub Hampl
The only way you can get it fancier is with MetaWhere.
让它变得更漂亮的唯一方法是使用MetaWhere。
MetaWhere has a newer cousin which is called Squeelwhich allows code like this:
MetaWhere 有一个名为Squeel的新表亲,它允许这样的代码:
GroupUser.where{user_id != me}
It goes without saying, that if this is the only refactor you are going to make, it is not worth using a gem and I would just stick with what you got. Squeel is useful in situations where you have many complex queries interacting with Ruby code.
不用说,如果这是您要进行的唯一重构,那么使用 gem 是不值得的,我会坚持使用您所得到的。Squeel 在您有许多复杂查询与 Ruby 代码交互的情况下很有用。
回答by Rick Smith
Rails 4:
导轨 4:
If you want to use bothnot equal and equal, you can use:
如果你想使用这两个不相等,平等,你可以使用:
user_id = 4
group_id = 27
GroupUser.where(group_id: group_id).where.not(user_id: user_id)
If you want to use a variety of operators (ie. >, <), at some point you may want to switch notations to the following:
如果您想使用各种运算符(即>, <),在某些时候您可能需要将符号切换为以下内容:
GroupUser.where("group_id > ? AND user_id != ?", group_id, user_id)
回答by Damien
You should alwaysinclude the table name in the SQL query when dealing with associations.
在处理关联时,您应该始终在 SQL 查询中包含表名。
Indeed if another table has the user_idcolumn and you join both tables, you will have an ambiguous column name in the SQL query (i.e. troubles).
实际上,如果另一个表具有该user_id列并且您加入了这两个表,则 SQL 查询中的列名将不明确(即麻烦)。
So, in your example:
所以,在你的例子中:
GroupUser.where("groups_users.user_id != ?", me)
Or a bit more verbose:
或者更详细一点:
GroupUser.where("#{table_name}.user_id IS NOT ?", me)
Note that if you are using a hash, you don't need to worry about that because Rails takes care of it for you:
请注意,如果您使用的是散列,则无需担心,因为 Rails 会为您处理它:
GroupUser.where(user: me)
In Rails 4, as said by @dr4k3, the query method nothas been added:
在 Rails 4 中,正如@dr4k3 所说,not添加了查询方法:
GroupUser.where.not(user: me)
回答by konyak
In Rails 3, I don't know anything fancier. However, I'm not sure if you're aware, your not equal condition does not match for (user_id) NULL values. If you want that, you'll have to do something like this:
在 Rails 3 中,我不知道有什么更好的了。但是,我不确定您是否知道,您的不相等条件与 (user_id) NULL 值不匹配。如果你想要那样,你必须做这样的事情:
GroupUser.where("user_id != ? OR user_id IS NULL", me)

