Skip to content
Browse files

Merge pull request #2007 from amatsuda/having_vargs_nomethoderror_fix

having() raises NoMethodError: undefined method `empty?' when the given argument does not respond to empty?
  • Loading branch information...
2 parents 111968d + 7c921fb commit bc364afb9333cb7729452adf09fc70ccac510226 @spastorino spastorino committed Jul 9, 2011
Showing with 15 additions and 8 deletions.
  1. +3 −3 activerecord/lib/active_record/relation/query_methods.rb
  2. +12 −5 activerecord/test/cases/finder_test.rb
View
6 activerecord/lib/active_record/relation/query_methods.rb
@@ -96,11 +96,11 @@ def where(opts, *rest)
relation
end
- def having(*args)
- return self if args.blank?
+ def having(opts, *rest)
+ return self if opts.blank?
relation = clone
- relation.having_values += build_where(*args)
+ relation.having_values += build_where(opts, rest)
relation
end
View
17 activerecord/test/cases/finder_test.rb
@@ -140,23 +140,30 @@ def test_find_all_with_limit_and_offset_and_multiple_order_clauses
def test_find_with_group
- developers = Developer.find(:all, :group => "salary", :select => "salary")
+ developers = Developer.find(:all, :group => "salary", :select => "salary")
assert_equal 4, developers.size
assert_equal 4, developers.map(&:salary).uniq.size
end
def test_find_with_group_and_having
- developers = Developer.find(:all, :group => "salary", :having => "sum(salary) > 10000", :select => "salary")
+ developers = Developer.find(:all, :group => "salary", :having => "sum(salary) > 10000", :select => "salary")
assert_equal 3, developers.size
assert_equal 3, developers.map(&:salary).uniq.size
- assert developers.all? { |developer| developer.salary > 10000 }
+ assert developers.all? { |developer| developer.salary > 10000 }
end
def test_find_with_group_and_sanitized_having
- developers = Developer.find(:all, :group => "salary", :having => ["sum(salary) > ?", 10000], :select => "salary")
+ developers = Developer.find(:all, :group => "salary", :having => ["sum(salary) > ?", 10000], :select => "salary")
assert_equal 3, developers.size
assert_equal 3, developers.map(&:salary).uniq.size
- assert developers.all? { |developer| developer.salary > 10000 }
+ assert developers.all? { |developer| developer.salary > 10000 }
+ end
+
+ def test_find_with_group_and_sanitized_having_method
+ developers = Developer.group(:salary).having("sum(salary) > ?", 10000).select('salary').all
+ assert_equal 3, developers.size
+ assert_equal 3, developers.map(&:salary).uniq.size
+ assert developers.all? { |developer| developer.salary > 10000 }
end
def test_find_with_entire_select_statement

0 comments on commit bc364af

Please sign in to comment.
Something went wrong with that request. Please try again.