mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Raise ActiveRecord::RecordNotSaved if an AssociationCollection fails to be replaced
This commit is contained in:
committed by
Aaron Patterson
parent
1d6e218428
commit
6055bbedaa
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user