Skip to content

Commit

Permalink
Ensure there won't be any regression with where(nil) calls
Browse files Browse the repository at this point in the history
Consider this scenario:

    if params[:foo]
      conditions = { foo: true }
    end

    foos = Foo.where(conditions).order(:id)

When params[:foo] is nil, this would call:

    foos = Foo.where(nil).order(:id)

In this scenario, we want Foo.where(conditions) to be the same as calling
Foo.all, otherwise we'd get a "NoMethodError order for WhereChain".

Related to rails#8332.
  • Loading branch information
carlosantoniodasilva committed Dec 7, 2012
1 parent 23b9cc8 commit 6ba0f97
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -452,8 +452,8 @@ def bind!(value) # :nodoc:
#
# 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?
def where(opts = :chain, *rest)
if opts == :chain
WhereChain.new(spawn)
elsif opts.blank?
self
Expand All @@ -464,8 +464,8 @@ def where(opts = nil, *rest)

# #where! is identical to #where, except that instead of returning a new relation, it adds
# the condition to the existing relation.
def where!(opts = nil, *rest) # :nodoc:
if opts.nil?
def where!(opts = :chain, *rest) # :nodoc:
if opts == :chain
WhereChain.new(self)
else
references!(PredicateBuilder.references(opts)) if Hash === opts
Expand Down
Expand Up @@ -299,7 +299,7 @@ def test_finding_array_compatibility
end

def test_find_with_blank_conditions
[[], {}, ""].each do |blank|
[[], {}, nil, ""].each do |blank|
assert_equal 2, Firm.all.merge!(:order => "id").first.clients.where(blank).to_a.size
end
end
Expand Down

0 comments on commit 6ba0f97

Please sign in to comment.