Rollback where.like and where.not_like
The real win with these chain methods is where.not, that takes care of different scenarios in a graceful way, for instance when the given value is nil. where("author.id != ?", author_to_ignore.id) where.not("author.id", author_to_ignore.id) Both where.like and where.not_like compared to the SQL versions doesn't seem to give us that much: Post.where("title LIKE 'ruby on%'") Post.where.like(title: 'ruby on%'") Post.where("title NOT LIKE 'ruby on%'") Post.where.not_like(title: 'ruby on%'") Thus Rails is adding where.not, but not where.like/not_like and others.
This commit is contained in:
parent
7b50dc5316
commit
8d02afeaee
@ -1,11 +1,10 @@
|
||||
## Rails 4.0.0 (unreleased) ##
|
||||
|
||||
* Allow `Relation#where` with no arguments to be chained with new query methods
|
||||
`not`, `like`, and `not_like`.
|
||||
* Allow `Relation#where` with no arguments to be chained with new `not` query method.
|
||||
|
||||
Example:
|
||||
|
||||
Developer.where.not(name: 'Aaron').where.like(name: 'Takoyaki%')
|
||||
Developer.where.not(name: 'Aaron')
|
||||
|
||||
*Akira Matsuda*
|
||||
|
||||
|
@ -47,32 +47,6 @@ def not(opts, *rest)
|
||||
@scope.where_values += where_value
|
||||
@scope
|
||||
end
|
||||
|
||||
# Returns a new relation expressing WHERE + LIKE condition
|
||||
# according to the conditions provided as a hash in the arguments.
|
||||
#
|
||||
# Book.where.like(title: "Rails%")
|
||||
# # SELECT * FROM books WHERE title LIKE 'Rails%'
|
||||
def like(opts, *rest)
|
||||
where_value = @scope.send(:build_where, opts, rest).map do |rel|
|
||||
Arel::Nodes::Matches.new(rel.left, rel.right)
|
||||
end
|
||||
@scope.where_values += where_value
|
||||
@scope
|
||||
end
|
||||
|
||||
# Returns a new relation expressing WHERE + NOT LIKE condition
|
||||
# according to the conditions provided as a hash in the arguments.
|
||||
#
|
||||
# Conference.where.not_like(name: "%Kaigi")
|
||||
# # SELECT * FROM conferences WHERE name NOT LIKE '%Kaigi'
|
||||
def not_like(opts, *rest)
|
||||
where_value = @scope.send(:build_where, opts, rest).map do |rel|
|
||||
Arel::Nodes::DoesNotMatch.new(rel.left, rel.right)
|
||||
end
|
||||
@scope.where_values += where_value
|
||||
@scope
|
||||
end
|
||||
end
|
||||
|
||||
Relation::MULTI_VALUE_METHODS.each do |name|
|
||||
|
@ -62,29 +62,14 @@ def test_not_eq_with_array_parameter
|
||||
assert_equal([expected], relation.where_values)
|
||||
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')
|
||||
relation = Post.where.not(author_id: [1, 2]).where.not(title: 'ruby on rails')
|
||||
|
||||
expected = Arel::Nodes::Matches.new(Post.arel_table[:title], 'ruby on %')
|
||||
expected = Arel::Nodes::NotIn.new(Post.arel_table[:author_id], [1, 2])
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user