Skip to content
Browse files

Merge pull request #1969 from dmitriy-kiriyenko/calculations_with_hav…

…ing_on_select

ActiveRecord calculation fail when having contains conditions based on select values
  • Loading branch information...
2 parents 24f0a87 + 9a298a1 commit 091e767c2db7a4791c4e617d40fb665ab73713cd @jonleighton jonleighton committed
View
1 activerecord/lib/active_record/relation/calculations.rb
@@ -250,6 +250,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
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}"
View
7 activerecord/test/cases/calculations_test.rb
@@ -170,6 +170,13 @@ def test_should_group_by_summed_field_having_sanitized_condition
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)]

0 comments on commit 091e767

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