diff options
Diffstat (limited to 'activerecord/test/cases/associations')
-rw-r--r-- | activerecord/test/cases/associations/join_model_test.rb | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index 2b929d4480..952ea63706 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -443,11 +443,33 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase assert_nil posts(:thinking).tags.find_by_name("General").attributes["tag_id"] end - def test_raise_error_when_adding_new_record_to_has_many_through - assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags << tags(:general).clone } - assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).clone.tags << tags(:general) } - assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags.build } - assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags.new } + def test_associating_unsaved_records_with_has_many_through + saved_post = posts(:thinking) + new_tag = Tag.new(:name => "new") + + saved_post.tags << new_tag + assert !new_tag.new_record? #consistent with habtm! + assert !saved_post.new_record? + assert saved_post.tags.include?(new_tag) + + assert !new_tag.new_record? + assert saved_post.reload.tags(true).include?(new_tag) + + + new_post = Post.new(:title => "Association replacmenet works!", :body => "You best believe it.") + saved_tag = tags(:general) + + new_post.tags << saved_tag + assert new_post.new_record? + assert !saved_tag.new_record? + assert new_post.tags.include?(saved_tag) + + new_post.save! + assert !new_post.new_record? + assert new_post.reload.tags(true).include?(saved_tag) + + assert posts(:thinking).tags.build.new_record? + assert posts(:thinking).tags.new.new_record? end def test_create_associate_when_adding_to_has_many_through |