From f6b12c11cd3a6df8525dd16ec093ec473813489e Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 6 Apr 2008 00:27:12 +0000 Subject: Refactor HasManyThroughAssociation to inherit from HasManyAssociation. Association callbacks and _ids= now work with hm:t. Closes #11516 [rubyruy] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9230 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../test/cases/associations/join_model_test.rb | 32 ++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'activerecord/test/cases/associations') 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 -- cgit v1.2.3