1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #1969 from dmitriy-kiriyenko/calculations_with_having_on_select

ActiveRecord calculation fail when having contains conditions based on select values
This commit is contained in:
Jon Leighton 2011-07-27 07:02:04 -07:00
commit 091e767c2d
2 changed files with 8 additions and 0 deletions

View file

@ -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}"

View file

@ -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)]