Ruby-on-rails Rails 5:ActiveRecord OR 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32753168/
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 5: ActiveRecord OR query
提问by K M Rakibul Islam
How do you do an orquery in Rails 5ActiveRecord? Also, is it possible to chain orwith wherein ActiveRecord queries?
你如何or在Rails 5ActiveRecord 中进行查询?此外,是否有可能链or与where在ActiveRecord的查询?
回答by K M Rakibul Islam
The ability to chain orclause along with whereclause in ActiveRecordquery will be available in Rails 5. See the related discussion and the pull request.
能够链接or连同条款where条款ActiveRecord查询将在现有的Rails 5。请参阅相关讨论和拉取请求。
So, you will be able to do the following things in Rails 5:
因此,您将能够在Rails 5 中执行以下操作:
To get a postwith id1 or 2:
为了得到一个post与id1或2:
Post.where('id = 1').or(Post.where('id = 2'))
Some other examples:
其他一些例子:
(A && B) || C:
(A && B) || C:
Post.where(a).where(b).or(Post.where(c))
(A || B) && C:
(A || B) && C:
Post.where(a).or(Post.where(b)).where(c)
回答by Dipak Gupta
We don't need to wait for rails 5 to use this ORquery. We can also used it with rails 4.2.3. There is a backport here.
我们不需要等待 rails 5 使用这个OR查询。我们也可以将它与rails 4.2.3. 有一个补丁包,在这里。
Thank to Eric-Guofor gem where-or, Now we can add this ORfunctionality in >= rails 4.2.3also using this gem.
感谢Eric-Guo提供 gem where-or,现在我们也可以使用这个 gem添加这个OR功能>= rails 4.2.3。
回答by Nicholas
(Just an addition to the answer by K M Rakibul Islam.)
(只是 KM Rakibul Islam 的回答的补充。)
Using scopes, the code can become prettier (depending on the eyes looking):
使用范围,代码可以变得更漂亮(取决于眼睛看):
scope a, -> {?where(a) }
scope b, -> { where(b) }
scope a_or_b, -> { a.or(b) }
回答by mtrolle
I needed to do a (A && B) || (C && D) || (E && F)
我需要做一个 (A && B) || (C && D) || (E && F)
But in Rails 5.1.4's current state this get's too complicated to accomplish with the Arel or-chain.
But I still wanted to use Rails to generate as much of the query as possible.
但是在 Rails5.1.4的当前状态下,这太复杂了,无法用 Arel or-chain 来完成。但我仍然想使用 Rails 生成尽可能多的查询。
So I made a small hack:
所以我做了一个小黑客:
In my model I created a privatemethod called sql_where:
在我的模型中,我创建了一个名为的私有方法sql_where:
private
def self.sql_where(*args)
sql = self.unscoped.where(*args).to_sql
match = sql.match(/WHERE\s(.*)$/)
"(#{match[1]})"
end
Next in my scope I created an array to hold the OR's
接下来在我的范围内,我创建了一个数组来保存 OR 的
scope :whatever, -> {
ors = []
ors << sql_where(A, B)
ors << sql_where(C, D)
ors << sql_where(E, F)
# Now just combine the stumps:
where(ors.join(' OR '))
}
Which will produce the expected query result:
SELECT * FROM `models` WHERE ((A AND B) OR (C AND D) OR (E AND F)).
这将产生预期的查询结果:
SELECT * FROM `models` WHERE ((A AND B) OR (C AND D) OR (E AND F))。
And now I can easily combine this with other scopes etc. without any wrongful OR's.
现在我可以轻松地将其与其他范围等结合起来,而不会出现任何错误的 OR。
The beauty being that my sql_where takes normal where-clause arguments:
sql_where(name: 'John', role: 'admin')will generate (name = 'John' AND role = 'admin').
美妙之处在于我的 sql_where 采用正常的 where-clause 参数:
sql_where(name: 'John', role: 'admin')将生成(name = 'John' AND role = 'admin').
回答by Foram Thakral
Rails 5 has ability for orclause with where.
For Example.
Rails 5 具有 foror子句的能力where。例如。
User.where(name: "abc").or(User.where(name: "abcd"))

