Browse files

Ensure there won't be any regression with where(nil) calls

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 #8332.
  • Loading branch information...
1 parent 23b9cc8 commit 6ba0f975d5d2867f80bcf8a809b1337c8f369383 @carlosantoniodasilva carlosantoniodasilva committed Dec 7, 2012
View
8 activerecord/lib/active_record/relation/query_methods.rb
@@ -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
@@ -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
View
2 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -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

0 comments on commit 6ba0f97

Please sign in to comment.