Raise ActiveRecord::RecordNotSaved if an AssociationCollection fails to be replaced

This commit is contained in:
Jon Leighton
2011-01-09 16:34:23 +00:00
committed by Aaron Patterson
parent 1d6e218428
commit 6055bbedaa
2 changed files with 18 additions and 1 deletions

View File

@@ -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

View File

@@ -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