forked from rails/rails
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relation.where with no args can be chained with not, like, and not_like
examples: Model.where.not field: nil #=> "SELECT * FROM models WHERE field IS NOT NULL Model.where.like name: 'Jeremy%' #=> "SELECT * FROM models WHERE name LIKE 'Jeremy%' this feature was originally suggested by Jeremy Kemper rails#5950 (comment) Closes rails#5950
- Loading branch information
Showing
5 changed files
with
198 additions
and
8 deletions.
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,78 @@ | |||
require 'cases/helper' | |||
require 'models/post' | |||
require 'models/comment' | |||
|
|||
module ActiveRecord | |||
class WhereChainTest < ActiveRecord::TestCase | |||
fixtures :posts | |||
|
|||
def test_not_eq | |||
expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'hello') | |||
relation = Post.where.not(title: 'hello') | |||
assert_equal([expected], relation.where_values) | |||
end | |||
|
|||
def test_not_null | |||
expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], nil) | |||
relation = Post.where.not(title: nil) | |||
assert_equal([expected], relation.where_values) | |||
end | |||
|
|||
def test_not_in | |||
expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], %w[hello goodbye]) | |||
relation = Post.where.not(title: %w[hello goodbye]) | |||
assert_equal([expected], relation.where_values) | |||
end | |||
|
|||
def test_association_not_eq | |||
expected = Arel::Nodes::NotEqual.new(Comment.arel_table[:title], 'hello') | |||
relation = Post.joins(:comments).where.not(comments: {title: 'hello'}) | |||
assert_equal(expected.to_sql, relation.where_values.first.to_sql) | |||
end | |||
|
|||
def test_not_eq_with_preceding_where | |||
relation = Post.where(title: 'hello').where.not(title: 'world') | |||
|
|||
expected = Arel::Nodes::Equality.new(Post.arel_table[:title], 'hello') | |||
assert_equal(expected, relation.where_values.first) | |||
|
|||
expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'world') | |||
assert_equal(expected, relation.where_values.last) | |||
end | |||
|
|||
def test_not_eq_with_succeeding_where | |||
relation = Post.where.not(title: 'hello').where(title: 'world') | |||
|
|||
expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'hello') | |||
assert_equal(expected, relation.where_values.first) | |||
|
|||
expected = Arel::Nodes::Equality.new(Post.arel_table[:title], 'world') | |||
assert_equal(expected, relation.where_values.last) | |||
end | |||
|
|||
def test_like | |||
expected = Arel::Nodes::Matches.new(Post.arel_table[:title], 'a%') | |||
relation = Post.where.like(title: 'a%') | |||
assert_equal([expected], relation.where_values) | |||
end | |||
|
|||
def test_not_like | |||
expected = Arel::Nodes::DoesNotMatch.new(Post.arel_table[:title], 'a%') | |||
relation = Post.where.not_like(title: 'a%') | |||
assert_equal([expected], relation.where_values) | |||
end | |||
|
|||
def test_chaining_multiple | |||
relation = Post.where.like(title: 'ruby on %').where.not(title: 'ruby on rails').where.not_like(title: '% ales') | |||
|
|||
expected = Arel::Nodes::Matches.new(Post.arel_table[:title], 'ruby on %') | |||
assert_equal(expected, relation.where_values[0]) | |||
|
|||
expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'ruby on rails') | |||
assert_equal(expected, relation.where_values[1]) | |||
|
|||
expected = Arel::Nodes::DoesNotMatch.new(Post.arel_table[:title], '% ales') | |||
assert_equal(expected, relation.where_values[2]) | |||
end | |||
end | |||
end |
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
de75af7
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.
👍! Now if we can also get greater than/less than, I'll be all set.
de75af7
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.
This looks great! :)
de75af7
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.
Wondering what if combining, like
User.where("age > 20").where.not(gender: "bisexual").where.not_like(name: "Mike%")
Does this work?