diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2011-07-09 05:20:42 -0700 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2011-07-09 05:20:42 -0700 |
commit | bc364afb9333cb7729452adf09fc70ccac510226 (patch) | |
tree | ed5e8450873ef013e3f84797bd74857952b624b1 | |
parent | 111968d4024fdccc386979551cdfc7799b39cff0 (diff) | |
parent | 7c921fbf1572698dd9541426b05b70d1c951cbe5 (diff) | |
download | rails-bc364afb9333cb7729452adf09fc70ccac510226.tar.gz rails-bc364afb9333cb7729452adf09fc70ccac510226.tar.bz2 rails-bc364afb9333cb7729452adf09fc70ccac510226.zip |
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?
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 17 |
2 files changed, 15 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index d17861f407..8bd4732c0c 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -96,11 +96,11 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 4e75eafe3d..3d2a03d2b9 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -140,23 +140,30 @@ class FinderTest < ActiveRecord::TestCase 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 |