Ruby on Rails 3 如何制作“或”条件

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

Ruby on Rails 3 howto make 'OR' condition

ruby-on-railsrubyruby-on-rails-3

提问by hjuskewycz

I need an SQL statement that check if one condition is satisfied:

我需要一个 SQL 语句来检查是否满足一个条件:

SELECT * FROM my_table WHERE my_table.x=1 OR my_table.y=1

I want to do this the 'Rails 3' way. I was looking for something like:

我想以“Rails 3”的方式做到这一点。我正在寻找类似的东西:

Account.where(:id => 1).or.where(:id => 2)

I know that I can always fallback to sql or a conditions string. However, in my experience this often leads to chaos when combining scopes. What is the best way to do this?

我知道我总是可以回退到 sql 或条件字符串。但是,根据我的经验,这在组合范围时通常会导致混乱。做这个的最好方式是什么?

Another related question, is how can describe relationship that depends on an OR condition. The only way I found:

另一个相关问题是如何描述依赖于 OR 条件的关系。我发现的唯一方法:

has_many :my_thing, :class_name => "MyTable",  :finder_sql => 'SELECT my_tables.* ' + 'FROM my_tables ' +
'WHERE my_tables.payer_id = #{id} OR my_tables.payee_id = #{id}'

However, these again breaks when used in combinations. IS there a better way to specify this?

但是,当组合使用时,它们又会中断。有没有更好的方法来指定这一点?

采纳答案by Jesse Wolgamott

Sadly, the .or isn't implemented yet (but when it is, it'll be AWESOME).

遗憾的是,.or 还没有实现(但是当它实现时,它会很棒)。

So you'll have to do something like:

因此,您必须执行以下操作:

class Project < ActiveRecord::Base
  scope :sufficient_data, :conditions=>['ratio_story_completion != 0 OR ratio_differential != 0']
  scope :profitable, :conditions=>['profit > 0']

That way you can still be awesome and do:

这样你仍然可以很棒并且做:

Project.sufficient_data.profitable

回答by Mike Campbell

Account.where(id: [1,2])no explanation needed.

Account.where(id: [1,2])不需要解释。

回答by weston

This will works in Rails 5, see rails master:

这将适用于 Rails 5,请参阅rails master

Post.where('id = 1').or(Post.where('id = 2'))
# => SELECT * FROM posts WHERE (id = 1) OR (id = 2)

For Rails 3.0.4+:

对于 Rails 3.0.4+:

accounts = Account.arel_table
Account.where(accounts[:id].eq(1).or(accounts[:id].eq(2)))

回答by David Morales

Those arel queries are unreadable to me.

这些 arel 查询对我来说是不可读的。

What's wrong with a SQL string? In fact, the Rails guides exposes this way as the first way to make conditions in queries: http://guides.rubyonrails.org/active_record_querying.html#array-conditions

SQL 字符串有什么问题?事实上,Rails 指南公开了这种方式作为在查询中创建条件的第一种方式:http: //guides.rubyonrails.org/active_record_querying.html#array-conditions

So, I bet for this way to do it as the "Rails way":

所以,我打赌这种方式是“Rails 方式”:

Account.where("id = 1 OR id = 2")

In my humble opinion, it's shorter and clearer.

在我看来,它更短更清晰。

回答by jpemberthy

I'd go with the INclause, e.g:

我会同意该IN条款,例如:

Account.where(["id in (?)", [1, 2]])

回答by brad

I've used the Squeel gem (https://github.com/ernie/squeel/) to do OR queries and it works beautifully.

我已经使用 Squeel gem ( https://github.com/ernie/squeel/) 来做 OR 查询,它运行得很好。

It lets you write your query as Account.where{(id == 1) | (id == 2)}

它允许您将查询编写为 Account.where{(id == 1) | (id == 2)}

回答by TehQuila

You can define an Array as value in the :conditionsHash.

您可以将数组定义为:conditions哈希中的值。

So you could do for example:

所以你可以做例如:

Account.all(:conditions => { :id => [1, 2] })

Tested with Rails 3.1.0

使用 Rails 3.1.0 测试

回答by Santhosh

Alternate syntax using Hash

使用 Hash 的替代语法

Account.where("id = :val1 OR id = :val2", val1: 1, val2: 2).

This is particularly useful, when the value is compared with multiple columns. eg:

当将值与多列进行比较时,这特别有用。例如:

User.where("first_name = :name OR last_name = :name", name: 'tom')

回答by khiav reoy

With rails_or, you could do it like:

使用rails_or,您可以这样做:

Account.where(id: 1).or(id: 2)

(It works in Rails 4 and 5, too.)

(它也适用于 Rails 4 和 5。)