Ruby-on-rails 不等于条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6003881/
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
Does not equal conditional
提问by user715697
I want to have a where clause with an equal and does not equal condition:
我想要一个具有相等和不相等条件的 where 子句:
@user = User.where(:user_id => current_user.id, :author_id != current_user.id).nil? ? (render :something) : (render :somethingelse)
The above does not work:
以上不起作用:
syntax error, unexpected ')', expecting tASSOC ...d, :user_id != current_user.id).nil? ? (render :index) : (re...
语法错误,意外的 ')',期待 tASSOC ...d,:user_id != current_user.id).nil?? (渲染:索引):(重新...
If I change the second condition from !=to =>it will work, however.
但是,如果我将第二个条件从!=改为=>,它将起作用。
How do I have both conditions in one where clase? Thank you
我如何在一个 where 类中同时拥有这两个条件?谢谢
回答by Tyler Rick
Here's how you would use Arel to generate the query "select * from users where user_id = ? and author_id != ?":
以下是使用 Arel 生成查询“ select * from users where user_id = ? and author_id != ?”的方法:
users = User.arel_table
User.where(users[:user_id]. eq(current_user.id).and(
users[:author_id].not_eq(current_user.id)))
Using Arel isn't as concise as using Hash conditions for simple conditions, but it's a lot more powerful!
对于简单条件,使用 Arel 不像使用 Hash 条件那么简洁,但它要强大得多!
Here's a link to the full list of predications(eq, not_eq, gt, lt, etc.) available with Arel.
下面是该链接断言的完整列表(eq,not_eq,gt,lt,等)可用阿雷尔。
回答by taro
I believe, it should be:
我相信,应该是:
@user = User.where(['user_id = ? AND author_id <> ?', current_user.id, current_user.id])
render(@user ? :something : :somethingelse)
回答by boulder_ruby
Rails 4 has this all figured out
Rails 4 已经解决了这一切
Model.where.not(:colname => nil)
#=> returns all records whose :colname values are not nil
回答by Douglas F Shearer
The syntax error is due to you attempting to use !=instead of =>. The wheremethod does not support inequality with hashed arguments, so your not equal will need to be written using array arguments.
语法错误是由于您尝试使用!=而不是=>. 该where方法不支持带有散列参数的不等式,因此您的不等式需要使用数组参数编写。
User.where(:user_id => current_user.id).where(['users.author_id <> ?', current_user.id])
回答by Jonah
http://guides.rubyonrails.org/active_record_querying.html#hash-conditions
http://guides.rubyonrails.org/active_record_querying.html#hash-conditions
Only equality, range and subset checking are possible with Hash conditions.
哈希条件只能进行相等、范围和子集检查。
You'll need to either drop down to straight SQL or invert and arel query, see Is there a way to invert an ActiveRecord::Relation query?
您需要下拉到直接 SQL 或反转和 arel 查询,请参阅有没有办法反转 ActiveRecord::Relation 查询?
回答by konyak
Not sure if you're aware, the not equal condition typically does not match (author_id) NULL values. You'll have to do an OR author_id IS NULLif you want that.
不确定您是否知道,不相等条件通常与 (author_id) NULL 值不匹配。OR author_id IS NULL如果你愿意,你必须做一个。
@users = User.where("user_id = ? AND (author_id != ? OR author_id IS NULL)",
current_user.id, current_user.id)
render(@users.present? ? :something : :somethingelse)
Also note that I'm using @users.present?because wherefinder returns an ActiveRecord::Relationarray.
另请注意,我使用的@users.present?是因为wherefinder 返回一个ActiveRecord::Relation数组。

