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

Distinct with order #count regression

This commit is contained in:
Max Schwenk 2018-02-25 19:54:20 -08:00
parent 065c1fae12
commit 5120bc90c2
No known key found for this signature in database
GPG key ID: 9CEA84EE71D901E1
2 changed files with 9 additions and 1 deletions

View file

@ -132,7 +132,7 @@ module ActiveRecord
if has_include?(column_name)
relation = apply_join_dependency
if operation.to_s.downcase == "count" && !distinct_value
if operation.to_s.downcase == "count"
relation.distinct!
# PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT
if (column_name == :all || column_name.nil?) && select_values.empty?

View file

@ -242,6 +242,14 @@ class CalculationsTest < ActiveRecord::TestCase
assert_queries(1) { assert_equal 11, posts.count(:all) }
end
def test_count_with_eager_loading_and_custom_order_and_distinct
posts = Post.includes(:comments).order("comments.id").distinct
assert_queries(1) { assert_equal 11, posts.count }
assert_queries(1) { assert_equal 11, posts.count(:all) }
assert_queries(1) { assert_equal 11, posts.size }
assert_queries(1) { assert_equal 11, posts.load.size }
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) }