aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-12-07 16:52:55 -0200
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-12-07 16:52:55 -0200
commit8d02afeaee8993bd0fde69687fdd9bf30921e805 (patch)
tree93cfbc3e5b18379c69abe6befa2bb3e78e542e86 /activerecord
parent7b50dc5316f1db83b2d138a12bb0b0ab5ea90a33 (diff)
downloadrails-8d02afeaee8993bd0fde69687fdd9bf30921e805.tar.gz
rails-8d02afeaee8993bd0fde69687fdd9bf30921e805.tar.bz2
rails-8d02afeaee8993bd0fde69687fdd9bf30921e805.zip
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.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb26
-rw-r--r--activerecord/test/cases/relation/where_chain_test.rb19
3 files changed, 4 insertions, 46 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 9798f783c1..82266fe1d5 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -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*
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 9484092430..f6d106f304 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -47,32 +47,6 @@ module ActiveRecord
@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|
diff --git a/activerecord/test/cases/relation/where_chain_test.rb b/activerecord/test/cases/relation/where_chain_test.rb
index edb3ea48b9..8ce44636b4 100644
--- a/activerecord/test/cases/relation/where_chain_test.rb
+++ b/activerecord/test/cases/relation/where_chain_test.rb
@@ -62,29 +62,14 @@ module ActiveRecord
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