diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2017-08-14 18:31:42 -0400 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2017-08-14 18:31:42 -0400 |
commit | 3fc47f4d82175c3f32dab6177685ae24f485c7b1 (patch) | |
tree | cb37ddb9973dc3a58720f6b3bc87e10bc1ccc02d /activerecord/test | |
parent | 6b632336c172ea397bb1386ff44c2e8320672cb4 (diff) | |
parent | 566f1fd068711dfe557bef63406f8dd6d41d473d (diff) | |
download | rails-3fc47f4d82175c3f32dab6177685ae24f485c7b1.tar.gz rails-3fc47f4d82175c3f32dab6177685ae24f485c7b1.tar.bz2 rails-3fc47f4d82175c3f32dab6177685ae24f485c7b1.zip |
Merge pull request #30169 from awortham/awortham/awortham/fix-sql-distinct-bug
Ensure sum honors distinct on has_many through
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/has_many_through_associations_test.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index f49383a1a8..968c0eb944 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -1125,6 +1125,32 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal ["parrot", "bulbul"], owner.toys.map { |r| r.pet.name } end + def test_has_many_through_associations_sum_on_columns + post1 = Post.create(title: "active", body: "sample") + post2 = Post.create(title: "inactive", body: "sample") + + person1 = Person.create(first_name: "aaron", followers_count: 1) + person2 = Person.create(first_name: "schmit", followers_count: 2) + person3 = Person.create(first_name: "bill", followers_count: 3) + person4 = Person.create(first_name: "cal", followers_count: 4) + + Reader.create(post_id: post1.id, person_id: person1.id) + Reader.create(post_id: post1.id, person_id: person2.id) + Reader.create(post_id: post1.id, person_id: person3.id) + Reader.create(post_id: post1.id, person_id: person4.id) + + Reader.create(post_id: post2.id, person_id: person1.id) + Reader.create(post_id: post2.id, person_id: person2.id) + Reader.create(post_id: post2.id, person_id: person3.id) + Reader.create(post_id: post2.id, person_id: person4.id) + + active_persons = Person.joins(:readers).joins(:posts).distinct(true).where("posts.title" => "active") + + assert_equal active_persons.map(&:followers_count).reduce(:+), 10 + assert_equal active_persons.sum(:followers_count), 10 + assert_equal active_persons.sum(:followers_count), active_persons.map(&:followers_count).reduce(:+) + end + def test_has_many_through_associations_on_new_records_use_null_relations person = Person.new |