aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb3
-rw-r--r--activerecord/test/associations_join_model_test.rb35
3 files changed, 27 insertions, 13 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 357f8a4d92..2a9a7c2042 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,6 +1,6 @@
*SVN*
-* Pushing a record onto a has_many :through sets the association's foreign key to the associate's primary key. #5815 [josh@hasmanythrough.com]
+* Pushing a record onto a has_many :through sets the association's foreign key to the associate's primary key and adds it to the correct association. #5815, #5829 [josh@hasmanythrough.com]
* PostgreSQL: simplify index introspection query. #5819 [stephen_purcell@yahoo.com]
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 0257b1857d..1962dc77ff 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -59,7 +59,8 @@ module ActiveRecord
raise_on_type_mismatch(associate)
raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new_record?) && !associate.new_record?
- @target << klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! }
+ @owner.send(@reflection.through_reflection.name).proxy_target << klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! }
+ @target << associate
end
end
diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb
index 1a0882fcfb..77908d4540 100644
--- a/activerecord/test/associations_join_model_test.rb
+++ b/activerecord/test/associations_join_model_test.rb
@@ -372,17 +372,30 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
def test_create_associate_when_adding_to_has_many_through
count = posts(:thinking).tags.count
push = Tag.create!(:name => 'pushme')
- assert_nothing_raised { posts(:thinking).tags << push }
- assert_equal(count + 1, posts(:thinking).tags.size)
- assert_equal(count + 1, posts(:thinking).tags(true).size)
-
- assert_nothing_raised { posts(:thinking).tags.create!(:name => 'foo') }
- assert_equal(count + 2, posts(:thinking).tags.size)
- assert_equal(count + 2, posts(:thinking).tags(true).size)
-
- assert_nothing_raised { posts(:thinking).tags.concat(Tag.create!(:name => 'abc'), Tag.create!(:name => 'def')) }
- assert_equal(count + 4, posts(:thinking).tags.size)
- assert_equal(count + 4, posts(:thinking).tags(true).size)
+ post_thinking = posts(:thinking)
+ assert_nothing_raised { post_thinking.tags << push }
+ assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag },
+ message = "Expected a Tag in tags collection, got #{wrong.class}.")
+ assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging },
+ message = "Expected a Tagging in taggings collection, got #{wrong.class}.")
+ assert_equal(count + 1, post_thinking.tags.size)
+ assert_equal(count + 1, post_thinking.tags(true).size)
+
+ assert_nothing_raised { post_thinking.tags.create!(:name => 'foo') }
+ assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag },
+ message = "Expected a Tag in tags collection, got #{wrong.class}.")
+ assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging },
+ message = "Expected a Tagging in taggings collection, got #{wrong.class}.")
+ assert_equal(count + 2, post_thinking.tags.size)
+ assert_equal(count + 2, post_thinking.tags(true).size)
+
+ assert_nothing_raised { post_thinking.tags.concat(Tag.create!(:name => 'abc'), Tag.create!(:name => 'def')) }
+ assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag },
+ message = "Expected a Tag in tags collection, got #{wrong.class}.")
+ assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging },
+ message = "Expected a Tagging in taggings collection, got #{wrong.class}.")
+ assert_equal(count + 4, post_thinking.tags.size)
+ assert_equal(count + 4, post_thinking.tags(true).size)
end
def test_adding_junk_to_has_many_through_should_raise_type_mismatch