From 144e060a6de90fda943fa427628cee94208bd25c Mon Sep 17 00:00:00 2001 From: Blake Smith Date: Tue, 21 Dec 2010 08:07:40 -0600 Subject: [PATCH] has_many :through _ids collection_reader methods should honor :uniq option --- activerecord/lib/active_record/associations.rb | 6 +++++- .../test/cases/associations/join_model_test.rb | 4 ++++ 2 files changed, 9 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 3a32581..257c594 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1316,7 +1316,11 @@ module ActiveRecord if send(reflection.name).loaded? || reflection.options[:finder_sql] send(reflection.name).map(&:id) else - send(reflection.name).all(:select => "#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map(&:id) + if reflection.options[:uniq] + send(reflection.name).all(:select => "DISTINCT #{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map(&:id) + else + send(reflection.name).all(:select => "#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map(&:id) + end end end end diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index 21412d1..db011a0 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -598,6 +598,10 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase assert_equal comment_ids.sort.reverse, authors(:david).ordered_uniq_comments_desc.map(&:id) end + def test_has_many_through_ids_should_honor_uniq + assert_equal 1, authors(:mary).unique_categorized_post_ids.size + end + def test_polymorphic_has_many expected = taggings(:welcome_general) p = Post.find(posts(:welcome).id, :include => :taggings) -- 1.7.3.1