diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-06-17 21:49:28 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2019-06-17 22:14:29 +0900 |
commit | cb0299c9eba6463085130debda6347abf9683463 (patch) | |
tree | ea35f99ba81e22ff028c047d65907fdbc0122ab2 /activerecord/test/cases | |
parent | b6068e6c42a070843b0a3844dc72c2a98ec3c6eb (diff) | |
download | rails-cb0299c9eba6463085130debda6347abf9683463.tar.gz rails-cb0299c9eba6463085130debda6347abf9683463.tar.bz2 rails-cb0299c9eba6463085130debda6347abf9683463.zip |
PostgreSQL: Fix GROUP BY with ORDER BY virtual count attribute
GROUP BY with virtual count attribute is invalid for almost all
databases, but it is valid for PostgreSQL, and it had worked until Rails
5.2.2, so it is a regression for Rails 5.2.3 (caused by 311f001).
I can't find perfectly solution for fixing this for now, but I would not
like to break existing apps, so I decided to allow referencing virtual
count attribute in ORDER BY clause when GROUP BY aggrigation (it partly
revert the effect of 311f001) to fix the regression #36022.
Fixes #36022.
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 4 |
2 files changed, 8 insertions, 2 deletions
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 5dabff1431..525085bb28 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -767,6 +767,12 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal [[2, 2], [4, 4]], Reply.includes(:topic).pluck(:id, :"topics.id") end + def test_group_by_with_order_by_virtual_count_attribute + expected = { "SpecialPost" => 1, "StiPost" => 2 } + actual = Post.group(:type).order(:count).limit(2).maximum(:comments_count) + assert_equal expected, actual + 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") diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index a5c162af33..5df1e3ccf9 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1955,8 +1955,8 @@ class RelationTest < ActiveRecord::TestCase test "joins with order by custom attribute" do companies = Company.create!([{ name: "test1" }, { name: "test2" }]) companies.each { |company| company.contracts.create! } - assert_equal companies, Company.joins(:contracts).order(:metadata) - assert_equal companies.reverse, Company.joins(:contracts).order(metadata: :desc) + assert_equal companies, Company.joins(:contracts).order(:metadata, :count) + assert_equal companies.reverse, Company.joins(:contracts).order(metadata: :desc, count: :desc) end test "delegations do not leak to other classes" do |