diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-01-09 16:34:23 +0000 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-01-11 13:45:07 -0800 |
commit | 6055bbedaa4b7b4bb2377ac87147196eebb2edc1 (patch) | |
tree | 0c7fb7848c25eed7d0ec9dd880f755c3b4e6cd3b | |
parent | 1d6e2184283d15d20ed3102ca462d905e5efa73d (diff) | |
download | rails-6055bbedaa4b7b4bb2377ac87147196eebb2edc1.tar.gz rails-6055bbedaa4b7b4bb2377ac87147196eebb2edc1.tar.bz2 rails-6055bbedaa4b7b4bb2377ac87147196eebb2edc1.zip |
Raise ActiveRecord::RecordNotSaved if an AssociationCollection fails to be replaced
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 13 |
2 files changed, 18 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index daec8493ac..3d8a23fdca 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -314,7 +314,11 @@ module ActiveRecord transaction do delete(@target - other_array) - concat(other_array - @target) + + unless concat(other_array - @target) + raise RecordNotSaved, "Failed to replace #{@reflection.name} because one or more of the " + "new records could not be saved." + end end end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 2b7ad3642a..1ce91d7211 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -975,6 +975,19 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert !firm.clients.include?(:first_client) end + def test_replace_failure + firm = companies(:first_firm) + account = Account.new + orig_accounts = firm.accounts.to_a + + assert !account.valid? + assert !orig_accounts.empty? + assert_raise ActiveRecord::RecordNotSaved do + firm.accounts = [account] + end + assert_equal orig_accounts, firm.accounts + end + def test_get_ids assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids end |