aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/calculations_test.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-12-20 05:09:49 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-12-20 06:41:10 +0900
commitc6cd9a59f200863ccfe8ad1d9c5a8876c39b9c5c (patch)
tree2fbbd9af309f7e01c43a6571744d824ad1f8ae97 /activerecord/test/cases/calculations_test.rb
parenteb6baccda26d65c70d0e2df28c6cd51ec3cdb2ac (diff)
downloadrails-c6cd9a59f200863ccfe8ad1d9c5a8876c39b9c5c.tar.gz
rails-c6cd9a59f200863ccfe8ad1d9c5a8876c39b9c5c.tar.bz2
rails-c6cd9a59f200863ccfe8ad1d9c5a8876c39b9c5c.zip
Fix `count(:all)` to correctly work `distinct` with custom SELECT list
Currently `count(:all)` with `distinct` doesn't work correctly because SELECT list is always replaced to `*` or primary key in that case even if having custom SELECT list. And also, PostgreSQL has a limitation that ORDER BY expressions must appear in select list for SELECT DISTINCT. Therefore, we should not replace custom SELECT list when using `count(:all)` with `distinct`. Closes #31277.
Diffstat (limited to 'activerecord/test/cases/calculations_test.rb')
-rw-r--r--activerecord/test/cases/calculations_test.rb6
1 files changed, 6 insertions, 0 deletions
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 55b50e4f84..5eda39a0c7 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -236,6 +236,12 @@ class CalculationsTest < ActiveRecord::TestCase
end
end
+ def test_distinct_count_all_with_custom_select_and_order
+ accounts = Account.distinct.select("credit_limit % 10").order(Arel.sql("credit_limit % 10"))
+ assert_queries(1) { assert_equal 3, accounts.count(:all) }
+ assert_queries(1) { assert_equal 3, accounts.load.size }
+ end
+
def test_distinct_count_with_order_and_limit
assert_equal 4, Account.distinct.order(:firm_id).limit(4).count
end