So in Rails 4 the long desired feature to use not
queries has been added.
因此,在Rails 4中,已经添加了希望使用而不是查询的功能。
Article.where.not(title: 'Rails 3')
Has similar support been added for or
queries, or are they planning to make it. I couldn't find anything by browsing through the release notes.
已经为或查询添加了类似的支持,或者他们正在计划这样做。通过浏览发行说明,我找不到任何东西。
Obviously I tried
显然我试着
Article.where(title: 'Rails 3').or(title: 'Rails 4')
But that does not work.
但这行不通。
7 个解决方案
#1
80
Article.where(title: ['Rails 3', 'Rails 4'])
is how you'd do that in Active Record.
篇文章。where(标题:['Rails 3', 'Rails 4'])是如何在活动记录中实现这一点的。
It's not possible to replicate any arbitrary SQL query using "Rails-y" syntax. But you can always just pass in literal sql.
使用“Rails-y”语法复制任意SQL查询是不可能的。但是,您总是可以直接传入文字sql。
So you could also do:
所以你也可以这样做:
Article.where("articles.title = 'Rails 3' OR articles.title = 'Rails 4'")
#2
32
This now works in Rails 5:
这在Rails 5中是有效的:
Article.where(title: 'Rails 3').or(Article.where(title: 'Rails 4'))
Code example in Rails's source.
Rails源代码中的代码示例。
#3
12
The most common alternative is already answer by @gregates
最常见的选择是@gregates
Recently there has been pull request in rails source
最近在rails源代码中出现了拉取请求
Add #any_of
query method to active_record Which adds or functionality to activerecord
在active_record中添加#any_of查询方法,该方法将添加或功能到activerecord。
the contributor has already created a gem incase its not accepted
贡献者已经创建了一个宝石,以防它不被接受
its rails 3.2 and 4 compatible
它的rails 3.2和4兼容。
https://github.com/oelmekki/activerecord_any_of
https://github.com/oelmekki/activerecord_any_of
I havent tried it yet but soon wish to use it looks good to me.
我还没试过,但我很快就想用它。
#4
4
It seems that the rails master branch is now supporting OR queries. https://github.com/rails/rails/pull/16052
看起来rails主分支现在支持或查询。https://github.com/rails/rails/pull/16052
I assume it will be in the framework with the next major release.
我认为它将在下一个主要版本的框架中。
#5
3
Rails 5 will support it but you can use this backport for Rails 4.2 : https://github.com/Eric-Guo/where-or
Rails 5将支持它,但是您可以将这个后台端口用于Rails 4.2: https://github.com/Eric-Guo/where-or
#6
3
I know that this is an old thread, but anyone else looking for a solution to this problem might find this code helpful:
我知道这是一个旧的线程,但是任何想要解决这个问题的人可能会发现这个代码很有用:
Article.where(title: ["Rails 3", "Rails 4"])
Maybe that will help you get someone going in the right direction without having to risk sql injection.
也许这将帮助您使某人朝着正确的方向前进,而不必冒sql注入的风险。
#7
1
You can use Squeel for more complex queries:
您可以使用Squeel进行更复杂的查询:
Article.where{(title == 'Rails 3') | (title == 'Rails 4')}
This results in the following SQL query:
这导致以下SQL查询:
SELECT `articles`.* FROM `articles` WHERE ((`articles`.`title` = 'Rails 3' OR `articles`.`title` = 'Rails 4'))
#1
80
Article.where(title: ['Rails 3', 'Rails 4'])
is how you'd do that in Active Record.
篇文章。where(标题:['Rails 3', 'Rails 4'])是如何在活动记录中实现这一点的。
It's not possible to replicate any arbitrary SQL query using "Rails-y" syntax. But you can always just pass in literal sql.
使用“Rails-y”语法复制任意SQL查询是不可能的。但是,您总是可以直接传入文字sql。
So you could also do:
所以你也可以这样做:
Article.where("articles.title = 'Rails 3' OR articles.title = 'Rails 4'")
#2
32
This now works in Rails 5:
这在Rails 5中是有效的:
Article.where(title: 'Rails 3').or(Article.where(title: 'Rails 4'))
Code example in Rails's source.
Rails源代码中的代码示例。
#3
12
The most common alternative is already answer by @gregates
最常见的选择是@gregates
Recently there has been pull request in rails source
最近在rails源代码中出现了拉取请求
Add #any_of
query method to active_record Which adds or functionality to activerecord
在active_record中添加#any_of查询方法,该方法将添加或功能到activerecord。
the contributor has already created a gem incase its not accepted
贡献者已经创建了一个宝石,以防它不被接受
its rails 3.2 and 4 compatible
它的rails 3.2和4兼容。
https://github.com/oelmekki/activerecord_any_of
https://github.com/oelmekki/activerecord_any_of
I havent tried it yet but soon wish to use it looks good to me.
我还没试过,但我很快就想用它。
#4
4
It seems that the rails master branch is now supporting OR queries. https://github.com/rails/rails/pull/16052
看起来rails主分支现在支持或查询。https://github.com/rails/rails/pull/16052
I assume it will be in the framework with the next major release.
我认为它将在下一个主要版本的框架中。
#5
3
Rails 5 will support it but you can use this backport for Rails 4.2 : https://github.com/Eric-Guo/where-or
Rails 5将支持它,但是您可以将这个后台端口用于Rails 4.2: https://github.com/Eric-Guo/where-or
#6
3
I know that this is an old thread, but anyone else looking for a solution to this problem might find this code helpful:
我知道这是一个旧的线程,但是任何想要解决这个问题的人可能会发现这个代码很有用:
Article.where(title: ["Rails 3", "Rails 4"])
Maybe that will help you get someone going in the right direction without having to risk sql injection.
也许这将帮助您使某人朝着正确的方向前进,而不必冒sql注入的风险。
#7
1
You can use Squeel for more complex queries:
您可以使用Squeel进行更复杂的查询:
Article.where{(title == 'Rails 3') | (title == 'Rails 4')}
This results in the following SQL query:
这导致以下SQL查询:
SELECT `articles`.* FROM `articles` WHERE ((`articles`.`title` = 'Rails 3' OR `articles`.`title` = 'Rails 4'))