aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitriy Kiriyenko <dmitriy.kiriyenko@gmail.com>2011-07-05 13:17:39 +0300
committerXavier Noria <fxn@hashref.com>2011-08-13 16:22:20 -0700
commit4c873cbf367d82538dbac914e7eee05b3582b6f1 (patch)
tree3ad18ee9a1e6e1f3cc78d07addb88dd9ed1bfbff
parent05f1a9bcc3c9223768187e2379b508638dfa19b6 (diff)
downloadrails-4c873cbf367d82538dbac914e7eee05b3582b6f1.tar.gz
rails-4c873cbf367d82538dbac914e7eee05b3582b6f1.tar.bz2
rails-4c873cbf367d82538dbac914e7eee05b3582b6f1.zip
Fixed failing query when performing calculation with having based on select.
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb1
-rw-r--r--activerecord/test/cases/calculations_test.rb7
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)]