diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-18 07:35:07 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-18 07:35:07 +0000 |
commit | 0da426be96d9e02b1b7a34e364dff984cd4218f3 (patch) | |
tree | de89954a3a945e2a8f7750684e4eeea1a9f05ee4 /activerecord/test | |
parent | 18057d2fb6017df988fadc6496f187c2e6208277 (diff) | |
download | rails-0da426be96d9e02b1b7a34e364dff984cd4218f3.tar.gz rails-0da426be96d9e02b1b7a34e364dff984cd4218f3.tar.bz2 rails-0da426be96d9e02b1b7a34e364dff984cd4218f3.zip |
Add records to has_many :through using <<, push, and concat by creating the association record. Raise if base or associate are new records since both ids are required to create the association. #build raises since you can't associate an unsaved record. #create! takes an attributes hash and creates the associated record and its association in a transaction.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4786 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/associations_join_model_test.rb | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index b5d48db8bc..508628f37a 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -363,14 +363,24 @@ class AssociationsJoinModelTest < Test::Unit::TestCase assert_nil posts(:thinking).tags.find_by_name("General").attributes["tag_id"] end - def test_raise_error_when_adding_to_has_many_through - assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags << tags(:general) } - assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags.push tags(:general) } - assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags.concat tags(:general) } - assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags.build(:name => 'foo') } - assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags.create(:name => 'foo') } + 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 } end - + + def test_create_associate_when_adding_to_has_many_through + count = Tagging.count + assert_nothing_raised { posts(:thinking).tags << tags(:general) } + assert_equal(count + 1, Tagging.count) + + assert_nothing_raised { posts(:thinking).tags.create!(:name => 'foo') } + assert_equal(count + 2, Tagging.count) + + assert_nothing_raised { posts(:thinking).tags.concat(Tag.create!(:name => 'abc'), Tag.create!(:name => 'def')) } + assert_equal(count + 4, Tagging.count) + end + def test_has_many_through_sum_uses_calculations assert_nothing_raised { authors(:david).comments.sum(:post_id) } end |