Permalink
Browse files

Raise ActiveRecord::RecordNotSaved if an AssociationCollection fails …

…to be replaced
  • Loading branch information...
jonleighton authored and tenderlove committed Jan 9, 2011
1 parent 1d6e218 commit 6055bbedaa4b7b4bb2377ac87147196eebb2edc1
@@ -314,7 +314,11 @@ def replace(other_array)
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 @@ def test_replace_with_new
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

0 comments on commit 6055bbe

Please sign in to comment.