Skip to content
Browse files

Fixed AssociationCollection#<< resulting in unexpected values in @target

 when :uniq => true

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
1 parent 4b2826a commit e7b00c113a71c8aafcca1974ef34d22da1bcd2b8 @ernie ernie committed with tarmo Aug 2, 2008
View
2 activerecord/lib/active_record/associations/association_collection.rb
@@ -338,7 +338,7 @@ def add_record_to_target_with_callbacks(record)
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
View
11 activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -299,6 +299,17 @@ def test_uniq_before_the_fact
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)
active_record = Project.find(1)

0 comments on commit e7b00c1

Please sign in to comment.
Something went wrong with that request. Please try again.