Skip to content

Commit 39449ef

Browse files
authored
Merge pull request rails#28183 from eugeneius/having_select_column
Include selects in group query with having clause
2 parents 0dfef8b + 1812568 commit 39449ef

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

activerecord/lib/active_record/relation/calculations.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
282282
operation,
283283
distinct).as(aggregate_alias)
284284
]
285+
select_values += self.select_values unless having_clause.empty?
285286

286287
select_values.concat group_columns.map { |aliaz, field|
287288
if field.respond_to?(:as)

activerecord/test/cases/calculations_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ def test_should_group_by_summed_field_having_condition
246246
end
247247

248248
def test_should_group_by_summed_field_having_condition_from_select
249-
c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("MIN(credit_limit) > 50").sum(:credit_limit)
249+
skip if current_adapter?(:PostgreSQLAdapter)
250+
c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("min_credit_limit > 50").sum(:credit_limit)
250251
assert_nil c[1]
251252
assert_equal 60, c[2]
252253
assert_equal 53, c[9]

0 commit comments

Comments
 (0)