diff options
author | Michael Koziarski <michael@koziarski.com> | 2007-09-18 10:26:56 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2007-09-18 10:26:56 +0000 |
commit | 7010ee361ec71ed1a37962897cebc8684d90aa35 (patch) | |
tree | b0d7d0b16037efc9c67fb87ccf0856bdc51974f2 /activerecord/test | |
parent | 4a1388adeab6aaf0ef28f975e480e22061bb1aad (diff) | |
download | rails-7010ee361ec71ed1a37962897cebc8684d90aa35.tar.gz rails-7010ee361ec71ed1a37962897cebc8684d90aa35.tar.bz2 rails-7010ee361ec71ed1a37962897cebc8684d90aa35.zip |
Stop users from calling .create on a has_many / habtm association when the owner is a new_record?
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7511 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-x | activerecord/test/associations_test.rb | 24 | ||||
-rwxr-xr-x | activerecord/test/validations_test.rb | 4 |
2 files changed, 24 insertions, 4 deletions
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index a1ea3f1c23..222cb5dbca 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -576,6 +576,26 @@ class HasManyAssociationsTest < Test::Unit::TestCase assert_equal 3, first_firm.plain_clients.length assert_equal 3, first_firm.plain_clients.size end + + def test_create_with_bang_on_has_many_when_parent_is_new_raises + assert_raises(ActiveRecord::RecordNotSaved) do + firm = Firm.new + firm.plain_clients.create! :name=>"Whoever" + end + end + + def test_regular_create_on_has_many_when_parent_is_new_raises + assert_raises(ActiveRecord::RecordNotSaved) do + firm = Firm.new + firm.plain_clients.create :name=>"Whoever" + end + end + + def test_create_with_bang_on_habtm_when_parent_is_new_raises + assert_raises(ActiveRecord::RecordNotSaved) do + Developer.new("name" => "Aredridel").projects.create! + end + end def test_adding_a_mismatch_class assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil } @@ -1540,8 +1560,8 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase def test_create_by_new_record devel = Developer.new(:name => "Marcel", :salary => 75000) - proj1 = devel.projects.create(:name => "Make bed") - proj2 = devel.projects.create(:name => "Lie in it") + proj1 = devel.projects.build(:name => "Make bed") + proj2 = devel.projects.build(:name => "Lie in it") assert_equal devel.projects.last, proj2 assert proj2.new_record? devel.save diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 8c7ef759e6..d0c54d1528 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -675,7 +675,7 @@ class ValidationsTest < Test::Unit::TestCase t = Topic.new('title' => 'noreplies', 'content' => 'whatever') assert !t.save assert t.errors.on(:replies) - t.replies.create('title' => 'areply', 'content' => 'whateveragain') + reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain') assert t.valid? end @@ -868,7 +868,7 @@ class ValidationsTest < Test::Unit::TestCase t = Topic.new('title' => 'あいうえお', 'content' => 'かきくけこ') assert !t.save assert t.errors.on(:replies) - t.replies.create('title' => 'あいうえお', 'content' => 'かきくけこ') + t.replies.build('title' => 'あいうえお', 'content' => 'かきくけこ') assert t.valid? end end |