diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-07-27 07:02:04 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-07-27 07:02:04 -0700 |
commit | 091e767c2db7a4791c4e617d40fb665ab73713cd (patch) | |
tree | cea21a5af39a744f182d5d419b9c76ca478557b2 | |
parent | 24f0a872e660647fe1abf187748660a77da3fe0a (diff) | |
parent | 9a298a162c16e019fe6971e563e7f4916e86ced6 (diff) | |
download | rails-091e767c2db7a4791c4e617d40fb665ab73713cd.tar.gz rails-091e767c2db7a4791c4e617d40fb665ab73713cd.tar.bz2 rails-091e767c2db7a4791c4e617d40fb665ab73713cd.zip |
Merge pull request #1969 from dmitriy-kiriyenko/calculations_with_having_on_select
ActiveRecord calculation fail when having contains conditions based on select values
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 1 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 7 |
2 files changed, 8 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 0ac821b2d7..9a7ff87e88 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -250,6 +250,7 @@ module ActiveRecord operation, distinct).as(aggregate_alias) ] + select_values += @select_values unless @having_values.empty? select_values.concat group_fields.zip(group_aliases).map { |field,aliaz| "#{field} AS #{aliaz}" diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 224b3f3d1f..42f98b3d42 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -170,6 +170,13 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal 60, c[2] end + def test_should_group_by_summed_field_having_condition_from_select + c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("min_credit_limit > 50").sum(:credit_limit) + assert_nil c[1] + assert_equal 60, c[2] + assert_equal 53, c[9] + end + def test_should_group_by_summed_association c = Account.sum(:credit_limit, :group => :firm) assert_equal 50, c[companies(:first_firm)] |