ActiveRecord OR 查询
如何在 Rails 3 ActiveRecord 中执行 OR 查询。我找到的所有示例都只有 AND 查询。
编辑:OR 方法自 Rails 5 起可用。请参阅 ActiveRecord::QueryMethods
How do you do an OR query in Rails 3 ActiveRecord. All the examples I find just have AND queries.
Edit: OR method is available since Rails 5. See ActiveRecord::QueryMethods
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
如果要对一列的值使用 OR 运算符,可以将数组传递给
.where
,ActiveRecord 将使用IN(value,other_value)
:输出:
这应该在单列上实现相当于
OR
的效果If you want to use an OR operator on one column's value, you can pass an array to
.where
and ActiveRecord will useIN(value,other_value)
:outputs:
This should achieve the equivalent of an
OR
on a single column在Rails 3中,应该是
这也包括原始sql,但我不认为ActiveRecord中有一种方法可以进行OR操作。你的问题不是菜鸟问题。
Rails 5 添加了 or,因此现在在 Rails 版本高于5:
这也处理
nil
值in Rails 3, it should be
This also includes raw sql but I dont think there is a way in ActiveRecord to do OR operation. Your question is not a noob question.
Rails 5 added or, so this is easier now in an app with Rails version greater than 5:
this handles
nil
values as well使用 ARel
生成的 SQL:
Use ARel
The resulting SQL:
Rails/ActiveRecord 的更新版本可能原生支持此语法。它看起来类似于:
正如此拉取请求中所述 https://github.com/rails/rails /pull/9052
目前,只需坚持以下效果就很好:
更新: 根据 https://github.com/rails/rails/pull/16052
or
功能将在 Rails 5 中提供更新: 功能已合并到 Rails 5 分支
An updated version of Rails/ActiveRecord may support this syntax natively. It would look similar to:
As noted in this pull request https://github.com/rails/rails/pull/9052
For now, simply sticking with the following works great:
Update: According to https://github.com/rails/rails/pull/16052 the
or
feature will be available in Rails 5Update: Feature has been merged to Rails 5 branch
Rails 最近已将其添加到 ActiveRecord 中。它看起来将在 Rails 5 中发布。已承诺掌握:
https://github.com/rails /rails/commit/9e42cf019f2417473e7dcbfcb885709fa2709f89
Rails has recently added this into ActiveRecord. It looks to be released in Rails 5. Committed to master already:
https://github.com/rails/rails/commit/9e42cf019f2417473e7dcbfcb885709fa2709f89
Rails 5 附带了一个
or
方法。 (l文档链接)此方法接受 <代码>ActiveRecord::Relation 对象。例如:
Rails 5 comes with an
or
method. (link to documentation)This method accepts an
ActiveRecord::Relation
object. eg:如果您想使用数组作为参数,以下代码可在 Rails 4 中使用:
更多信息:OR 在 Rails 4 中使用数组作为参数的查询。
If you want to use arrays as arguments, the following code works in Rails 4:
More information: OR queries with arrays as arguments in Rails 4.
MetaWhere 插件非常棒。
轻松混合 OR 和 AND,连接任何关联的条件,甚至指定 OUTER JOIN!
The MetaWhere plugin is completely amazing.
Easily mix OR's and AND's, join conditions on any association, and even specify OUTER JOIN's!
只需在条件中添加 OR 即可
Just add an OR in the conditions
使用rails + arel,更清晰的方式:
产生:
With rails + arel, a more clear way:
Produces:
你可以这样做:
或更优雅的是,安装 rails_or gem 并执行以下操作:
You could do it like:
or more elegant, install rails_or gem and do it like:
我刚刚从客户工作中提取了这个插件,它可以让您将范围与
.or结合起来。
,例如。Post.published.or.authored_by(current_user)
。 Squeel(MetaSearch 的较新实现)也很棒,但不允许您使用 OR 范围,因此查询逻辑可能会有点冗余。I just extracted this plugin from client work that lets you combine scopes with
.or.
, ex.Post.published.or.authored_by(current_user)
. Squeel (newer implementation of MetaSearch) is also great, but doesn't let you OR scopes, so query logic can get a bit redundant.我想补充一下,这是一个搜索 ActiveRecord 的多个属性的解决方案。因为
将搜索 A 和 B。
I'd like to add this is a solution to search multiple attributes of an ActiveRecord. Since
will search for A and B.
使用
activerecord_any_of
gem,您可以编写Using the
activerecord_any_of
gem, you can write