Ruby-on-rails ActiveRecord OR 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3639656/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:09:09  来源:igfitidea点击:

ActiveRecord OR query

ruby-on-railsruby-on-rails-3rails-activerecord

提问by pho3nixf1re

How do you do an OR query in Rails 3 ActiveRecord. All the examples I find just have AND queries.

您如何在 Rails 3 ActiveRecord 中执行 OR 查询。我发现的所有示例都只有 AND 查询。

Edit: ORmethod is available since Rails 5. See ActiveRecord::QueryMethods

编辑:OR方法自 Rails 5 起可用。请参阅ActiveRecord::QueryMethods

采纳答案by Dan McNevin

Use ARel

使用ARel

t = Post.arel_table

results = Post.where(
  t[:author].eq("Someone").
  or(t[:title].matches("%something%"))
)

The resulting SQL:

生成的 SQL:

ree-1.8.7-2010.02 > puts Post.where(t[:author].eq("Someone").or(t[:title].matches("%something%"))).to_sql
SELECT     "posts".* FROM       "posts"  WHERE     (("posts"."author" = 'Someone' OR "posts"."title" LIKE '%something%'))

回答by deadkarma

If you want to use an OR operator on one column's value, you can pass an array to .whereand ActiveRecord will use IN(value,other_value):

如果要对一列的值使用 OR 运算符,可以将数组传递给.whereActiveRecord 将使用IN(value,other_value)

Model.where(:column => ["value", "other_value"]

outputs:

输出:

SELECT `table_name`.* FROM `table_name` WHERE `table_name`.`column` IN ('value', 'other_value')

This should achieve the equivalent of an ORon a single column

这应该相当于OR单列上的

回答by rubyprince

in Rails 3, it should be

在 Rails 3 中,它应该是

Model.where("column = ? or other_column = ?", value, other_value)

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.

这也包括原始 sql,但我认为 ActiveRecord 中没有办法进行 OR 操作。你的问题不是菜鸟问题。

回答by Christian Fazzini

An updated version of Rails/ActiveRecord may support this syntax natively. It would look similar to:

Rails/ActiveRecord 的更新版本可能本身就支持这种语法。它看起来类似于:

Foo.where(foo: 'bar').or.where(bar: 'bar')

As noted in this pull request https://github.com/rails/rails/pull/9052

如本拉取请求中所述https://github.com/rails/rails/pull/9052

For now, simply sticking with the following works great:

就目前而言,只需坚持以下工作即可:

Foo.where('foo= ? OR bar= ?', 'bar', 'bar')

Update:According to https://github.com/rails/rails/pull/16052the orfeature will be available in Rails 5

更新:根据https://github.com/rails/rails/pull/16052,or功能将在 Rails 5 中可用

Update:Feature has been merged to Rails 5 branch

更新:功能已合并到 Rails 5 分支

回答by Greg Olsen

Rails has recently added this into ActiveRecord. It looks to be released in Rails 5. Committed to master already:

Rails 最近将它添加到 ActiveRecord 中。它看起来在 Rails 5 中发布。 已经致力于掌握:

https://github.com/rails/rails/commit/9e42cf019f2417473e7dcbfcb885709fa2709f89

https://github.com/rails/rails/commit/9e42cf019f2417473e7dcbfcb885709fa2709f89

Post.where(column: 'something').or(Post.where(other: 'else'))

# => SELECT * FROM posts WHERE (column = 'something') OR (other = 'else)

回答by Santhosh

Rails 5 comes with an ormethod. (link to documentation)

Rails 5 附带了一个or方法。(链接到文档

This method accepts an ActiveRecord::Relationobject. eg:

此方法接受一个ActiveRecord::Relation对象。例如:

User.where(first_name: 'James').or(User.where(last_name: 'Scott'))

回答by Rafa? Cie?lak

If you want to use arrays as arguments, the following code works in Rails 4:

如果您想使用数组作为参数,以下代码适用于 Rails 4:

query = Order.where(uuid: uuids, id: ids)
Order.where(query.where_values.map(&:to_sql).join(" OR "))
#=> Order Load (0.7ms)  SELECT "orders".* FROM "orders" WHERE ("orders"."uuid" IN ('5459eed8350e1b472bfee48375034103', '21313213jkads', '43ujrefdk2384us') OR "orders"."id" IN (2, 3, 4))

More information: OR queries with arrays as arguments in Rails 4.

更多信息:在 Rails 4 中使用数组作为参数的 OR 查询

回答by Duke

The MetaWhereplugin is completely amazing.

MetaWhere插件完全是惊人的。

Easily mix OR's and AND's, join conditions on any association, and even specify OUTER JOIN's!

轻松混合 OR 和 AND,连接任何关联的条件,甚至指定 OUTER JOIN!

Post.where({sharing_level: Post::Sharing[:everyone]} | ({sharing_level: Post::Sharing[:friends]} & {user: {followers: current_user} }).joins(:user.outer => :followers.outer}

回答by Toby Hede

Just add an OR in the conditions

只需在条件中添加一个 OR

Model.find(:all, :conditions => ["column = ? OR other_column = ?",value, other_value])

回答by Woahdae

I just extracted this pluginfrom 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.

我刚刚从客户端工作中提取了这个插件,它允许您将范围与.or.,例如。Post.published.or.authored_by(current_user). Squeel(MetaSearch 的较新实现)也很棒,但不允许您使用 OR 范围,因此查询逻辑可能有点多余。