diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-07-20 12:13:11 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-07-22 08:40:16 +0900 |
commit | 0df0dfbfac91c2cc4dce1ae73d4026121d109275 (patch) | |
tree | fcb94a632f73860e730695df9676c4c57e56a371 /activerecord | |
parent | a265d4b29cf9c1be84603ec53a6f8b17b53321a9 (diff) | |
download | rails-0df0dfbfac91c2cc4dce1ae73d4026121d109275.tar.gz rails-0df0dfbfac91c2cc4dce1ae73d4026121d109275.tar.bz2 rails-0df0dfbfac91c2cc4dce1ae73d4026121d109275.zip |
Should keep the table name qualified `*` for distinct subquery
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 12 |
2 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index a4714f138a..d281f8673f 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -388,7 +388,7 @@ module ActiveRecord def build_count_subquery(relation, column_name, distinct) relation.select_values = [ if column_name == :all - distinct ? Arel.star : Arel.sql("1") + distinct ? table[Arel.star] : Arel.sql("1") else column_alias = Arel.sql("count_column") aggregate_column(column_name).as(column_alias) diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 0541b78f0f..92d071187d 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -248,6 +248,18 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal 4, Account.distinct.order(:firm_id).limit(4).offset(2).count end + def test_distinct_joins_count_with_order_and_limit + assert_equal 3, Account.joins(:firm).distinct.order(:firm_id).limit(3).count + end + + def test_distinct_joins_count_with_order_and_offset + assert_equal 3, Account.joins(:firm).distinct.order(:firm_id).offset(2).count + end + + def test_distinct_joins_count_with_order_and_limit_and_offset + assert_equal 3, Account.joins(:firm).distinct.order(:firm_id).limit(3).offset(2).count + end + def test_should_group_by_summed_field_having_condition c = Account.group(:firm_id).having("sum(credit_limit) > 50").sum(:credit_limit) assert_nil c[1] |