Skip to content
This repository
Browse code

Fixed failing query when performing calculation with having based on …

…select.
  • Loading branch information...
commit 9a298a162c16e019fe6971e563e7f4916e86ced6 1 parent 24f0a87
Dmitriy Kiriyenko dmitriy-kiriyenko authored
1  activerecord/lib/active_record/relation/calculations.rb
@@ -250,6 +250,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
250 250 operation,
251 251 distinct).as(aggregate_alias)
252 252 ]
  253 + select_values += @select_values unless @having_values.empty?
253 254
254 255 select_values.concat group_fields.zip(group_aliases).map { |field,aliaz|
255 256 "#{field} AS #{aliaz}"
7 activerecord/test/cases/calculations_test.rb
@@ -170,6 +170,13 @@ def test_should_group_by_summed_field_having_sanitized_condition
170 170 assert_equal 60, c[2]
171 171 end
172 172
  173 + def test_should_group_by_summed_field_having_condition_from_select
  174 + c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("min_credit_limit > 50").sum(:credit_limit)
  175 + assert_nil c[1]
  176 + assert_equal 60, c[2]
  177 + assert_equal 53, c[9]
  178 + end
  179 +
173 180 def test_should_group_by_summed_association
174 181 c = Account.sum(:credit_limit, :group => :firm)
175 182 assert_equal 50, c[companies(:first_firm)]

0 comments on commit 9a298a1

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