forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ActiveRecord::Relation#or returns a new relation, which is the result of filtering the current relation according to the conditions in the arguments, joining WHERE clauses with OR operand, contraty to the default behaviour that uses AND. ActiveRecord::Relation#or accepts conditions in one of several formats. In the examples below, the resulting SQL is given as an illustration; the actual query generated may be different depending on the database adapter. * without arguments If ActiveRecord::Relation#or is used without arguments, it returns an ActiveRecord::OrChain object that can be used to chain queries with any other relation method, like where: Post.where("id = 1").or.where("id = 2") > SELECT `posts`.* FROM `posts` WHERE (('id = 1' OR 'id = 2')) It can also be chained with a named scope: Post.where("id = 1").or.containing_the_letter_a > SELECT `posts`.* FROM `posts` WHERE (('id = 1' OR 'body LIKE \\'%a%\\'')) * ActiveRecord::Relation When #or is used with an ActiveRecord::Relation as an argument, it merges the two relations, with the exception of the WHERE clauses, that are joined using the OR operand. Post.where("id = 1").or(Post.where("id = 2")) > SELECT `posts`.* FROM `posts` WHERE (('id = 1' OR 'id = 2')) * anything you would pass to #where ActiveRecord::Relation#or also accepts anything that could be passed to the #where method, as a shortcut: Post.where("id = 1").or("id = ?", 2) > SELECT `posts`.* FROM `posts` WHERE (('id = 1' OR 'id = 2'))
- Loading branch information
1 parent
fb3996f
commit 2366cc5
Showing
4 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require "cases/helper" | ||
require 'models/post' | ||
|
||
module ActiveRecord | ||
class OrTest < ActiveRecord::TestCase | ||
fixtures :posts | ||
|
||
def test_or_with_relation | ||
expected = Post.where('id = 1 or id = 2').to_a | ||
assert_equal expected, Post.where('id = 1').or(Post.where('id = 2')).to_a | ||
end | ||
|
||
def test_or_with_string | ||
expected = Post.where('id = 1 or id = 2').to_a | ||
assert_equal expected, Post.where('id = 1').or('id = 2').to_a | ||
end | ||
|
||
def test_or_chaining | ||
expected = Post.where('id = 1 or id = 2').to_a | ||
assert_equal expected, Post.where('id = 1').or.where('id = 2').to_a | ||
end | ||
|
||
def test_or_without_left_where | ||
expected = Post.where('id = 1').to_a | ||
assert_equal expected, Post.or('id = 1').to_a | ||
end | ||
|
||
def test_or_without_right_where | ||
expected = Post.where('id = 1').to_a | ||
assert_equal expected, Post.where('id = 1').or(Post.all).to_a | ||
end | ||
|
||
def test_or_preserves_other_querying_methods | ||
expected = Post.where('id = 1 or id = 2 or id = 3').order('body asc').to_a | ||
assert_equal expected, Post.where('id = 1').order('body asc').or(:id => [2, 3]).to_a | ||
end | ||
|
||
def test_or_with_named_scope | ||
expected = Post.where("id = 1 or body LIKE '\%a\%'").to_a | ||
assert_equal expected, Post.where('id = 1').or.containing_the_letter_a | ||
end | ||
|
||
def test_or_on_loaded_relation | ||
expected = Post.where('id = 1 or id = 2').to_a | ||
p = Post.where('id = 1') | ||
p.load | ||
assert_equal p.loaded?, true | ||
assert_equal expected, p.or('id = 2').to_a | ||
end | ||
|
||
end | ||
end |
2366cc5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I converted this into something that could be used as a monkey patch since the rails team doesnt seem to want to support this. I really needed it. If anyone else does add the following gist as an initializer.
https://gist.github.com/j-mcnally/250eaaceef234dd8971b