diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index b80c3c916f39c2962e2f349f65a847bffd722484..683d544b77c863d1df1fc910ce1430dde584bbdd 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -136,7 +136,7 @@ def calculate(operation, column_name) relation.select_values = [ klass.primary_key || table[Arel.star] ] end # PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT - relation.order_values = [] + relation.order_values = [] if group_values.empty? end relation.calculate(operation, column_name) diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 839b1882c6ae2b1408e4f323b0bcdc327f83a43c..37a87c3fb838627a289e32256aed707be658e10a 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -851,20 +851,20 @@ def test_group_by_with_order_by_virtual_count_attribute end if current_adapter?(:PostgreSQLAdapter) def test_group_by_with_limit - expected = { "Post" => 8, "SpecialPost" => 1 } - actual = Post.includes(:comments).group(:type).order(:type).limit(2).count("comments.id") + expected = { "StiPost" => 2, "SpecialPost" => 1 } + actual = Post.includes(:comments).group(:type).order(type: :desc).limit(2).count("comments.id") assert_equal expected, actual end def test_group_by_with_offset - expected = { "SpecialPost" => 1, "StiPost" => 2 } - actual = Post.includes(:comments).group(:type).order(:type).offset(1).count("comments.id") + expected = { "SpecialPost" => 1, "Post" => 8 } + actual = Post.includes(:comments).group(:type).order(type: :desc).offset(1).count("comments.id") assert_equal expected, actual end def test_group_by_with_limit_and_offset expected = { "SpecialPost" => 1 } - actual = Post.includes(:comments).group(:type).order(:type).offset(1).limit(1).count("comments.id") + actual = Post.includes(:comments).group(:type).order(type: :desc).offset(1).limit(1).count("comments.id") assert_equal expected, actual end