In revision r9248, there is a test_update_attributes_after_push_without_duplicate_join_table_rows
in has_and_belongs_to_many_associations_test.rb that addresses ticket http://dev.rubyonrails.org/ticke...
This test verifies that duplicate rows are not created when duplicate objects are pushed into the habtm association one by one with 'save' called in between. For example, no duplicate rows are created for the following:
developer = Developer.new("name" => "Kano")
project = SpecialProject.create("name" => "Special Project")
assert developer.save
assert_equal 1, developer.projects.size
developer = Developer.new("name" => "Kano")
project = SpecialProject.create("name" => "Special Project")
assert developer.save
assert_equal 1, developer.projects.size
However, when a list of duplicate objects are pushed into the habtm association at the same time without 'save' called in between the elements of the list, duplicate rows are introduced into the join table. For example:
developer = Developer.new("name" => "Kano")
project = SpecialProject.create("name" => "Special Project")
developer.projects << [project, project]
-
developer.projects.size is 2 instead of 1
-
following test will fail:
assert_equal 1, developer.projects.size
Based on what the test for ticket 2888 says, it seems like this isn't how '<<' is supposed to behave. Is this intentional for some reason? Calling save for each push could result in a lot of trips to the db.