From 6fc28d025b61080d9fb89b897a2af3a480b95f4b Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Sat, 2 Aug 2008 11:47:05 -0400 Subject: [PATCH] Fixed AssociationCollection#<< resulting in unexpected values in @target when :uniq => true --- .../associations/association_collection.rb | 2 +- .../has_and_belongs_to_many_associations_test.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index a28be9e..9061037 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -344,7 +344,7 @@ module ActiveRecord callback(:before_add, record) yield(record) if block_given? @target ||= [] unless loaded? - @target << record + @target << record unless @reflection.options[:uniq] && @target.include?(record) callback(:after_add, record) record end diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index b29df68..d68cf4c 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -298,6 +298,17 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase projects(:active_record).developers << developers(:david) assert_equal 3, projects(:active_record, :reload).developers.size end + + def test_uniq_option_prevents_duplicate_push + project = projects(:active_record) + project.developers << developers(:jamis) + project.developers << developers(:david) + assert_equal 3, project.developers.size + + project.developers << developers(:david) + project.developers << developers(:jamis) + assert_equal 3, project.developers.size + end def test_deleting david = Developer.find(1) -- 1.5.6.2