Skip to content

Commit

Permalink
Relation.where with no args can be chained with not, like, and not_like
Browse files Browse the repository at this point in the history
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 #5950 (comment)

Closes #5950
  • Loading branch information
amatsuda committed Nov 29, 2012
1 parent 80b8df5 commit de75af7
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 8 deletions.
9 changes: 9 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,14 @@
## Rails 4.0.0 (unreleased) ##

* Allow `Relation#where` with no arguments to be chained with new query methods
`not`, `like`, and `not_like`.

Example:

Developer.where.not(name: 'Aaron').where.like(name: 'Takoyaki%')

*Akira Matsuda*

* Fix dirty attribute checks for TimeZoneConversion with nil and blank
datetime attributes. Setting a nil datetime to a blank string should not
result in a change being flagged. Fix #8310
Expand Down
103 changes: 96 additions & 7 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -4,6 +4,68 @@ module ActiveRecord
module QueryMethods
extend ActiveSupport::Concern

# WhereChain objects act as placeholder for queries in which #where does not have any parameter.
# In this case, #where must be chained with either #not, #like, or #not_like to return a new relation.
class WhereChain
def initialize(scope)
@scope = scope
end

# Returns a new relation expressing WHERE + NOT condition
# according to the conditions in the arguments.
#
# User.where.not(name: "Jon")
# # SELECT * FROM users WHERE name <> 'Jon'
#
# User.where.not(name: nil)
# # SELECT * FROM users WHERE name IS NOT NULL
#
# User.where.not(name: %(Ko1 Nobu))
# # SELECT * FROM users WHERE name NOT IN ('Ko1', 'Nobu')
def not(opts, *rest)
where_value = @scope.send(:build_where, opts, rest).map do |rel|
case rel
when Arel::Nodes::Equality
Arel::Nodes::NotEqual.new(rel.left, rel.right)
when Arel::Nodes::In
Arel::Nodes::NotIn.new(rel.left, rel.right)
when String
Arel::Nodes::Not.new(Arel::Nodes::SqlLiteral.new(rel))
else
Arel::Nodes::Not.new(rel)
end
end
@scope.where_values += where_value
@scope
end

# Returns a new relation expressing WHERE + LIKE condition
# according to the conditions 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 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|
class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name}_values # def select_values
Expand Down Expand Up @@ -379,20 +441,47 @@ def bind!(value)
# User.joins(:posts).where({ "posts.published" => true })
# User.joins(:posts).where({ posts: { published: true } })
#
# === no argument or nil
#
# If no argument or nil is passed, #where returns a new instance of WhereChain which, when
# chained with either #not, #like, or #not_like, returns a new relation.
#
# User.where.not(name: "Jon")
# # SELECT * FROM users WHERE name <> 'Jon'
#
# Book.where.like(title: "Rails%")
# # SELECT * FROM books WHERE title LIKE 'Rails%'
#
# Conference.where.not_like(name: "%Kaigi")
# # SELECT * FROM conferences WHERE name NOT LIKE '%Kaigi'
#
# See WhereChain for more details on #not, #like, and #not_like.
#
# === empty condition
#
# If the condition returns true for blank?, then where is a no-op and returns the current relation.
def where(opts, *rest)
opts.blank? ? self : spawn.where!(opts, *rest)
# If the condition is any other blank-ish object than nil, then where is a # no-op and returns
# the current relation.
def where(opts = nil, *rest)
if opts.nil?
WhereChain.new(spawn)
elsif opts.blank?
self
else
spawn.where!(opts, *rest)
end
end

# #where! is identical to #where, except that instead of returning a new relation, it adds
# the condition to the existing relation.
def where!(opts, *rest)
references!(PredicateBuilder.references(opts)) if Hash === opts
def where!(opts = nil, *rest)
if opts.nil?
WhereChain.new(self)
else
references!(PredicateBuilder.references(opts)) if Hash === opts

self.where_values += build_where(opts, rest)
self
self.where_values += build_where(opts, rest)
self
end
end

# Allows to specify a HAVING clause. Note that you can't use HAVING
Expand Down
Expand Up @@ -271,7 +271,7 @@ def test_finding_array_compatibility
end

def test_find_with_blank_conditions
[[], {}, nil, ""].each do |blank|
[[], {}, ""].each do |blank|
assert_equal 2, Firm.all.merge!(:order => "id").first.clients.where(blank).to_a.size
end
end
Expand Down
78 changes: 78 additions & 0 deletions activerecord/test/cases/relation/where_chain_test.rb
@@ -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
14 changes: 14 additions & 0 deletions guides/source/active_record_querying.md
Expand Up @@ -503,6 +503,20 @@ This code will generate SQL like this:
SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))
```

### NOT, LIKE, and NOT LIKE Conditions

`NOT`, `LIKE`, and `NOT LIKE` SQL queries can be built by `where.not`, `where.like`, and `where.not_like` respectively.

```ruby
Post.where.not(author: author)

Author.where.like(name: 'Nari%')

Developer.where.not_like(name: 'Tenderl%')
```

In other words, these sort of queries can be generated by calling `where` with no argument, then immediately chain with `not`, `like`, or `not_like` passing `where` conditions.

Ordering
--------

Expand Down

0 comments on commit de75af7

Please sign in to comment.