From 7df61754a5e66ed01020800bcc9903d934dbbb7b Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Mon, 13 Sep 2010 14:56:42 -0300 Subject: Remove associated records from identity map if any raised an unexpected exception. --- .../lib/active_record/autosave_association.rb | 33 +++++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 2b534f53ad..f417587399 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -320,22 +320,27 @@ module ActiveRecord autosave = reflection.options[:autosave] if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave) - records.each do |record| - next if record.destroyed? - - if autosave && record.marked_for_destruction? - association.destroy(record) - elsif autosave != false && (@new_record_before_save || !record.persisted?) - if autosave - saved = association.send(:insert_record, record, false, false) - else - association.send(:insert_record, record) + begin + records.each do |record| + next if record.destroyed? + + if autosave && record.marked_for_destruction? + association.destroy(record) + elsif autosave != false && (@new_record_before_save || !record.persisted?) + if autosave + saved = association.send(:insert_record, record, false, false) + else + association.send(:insert_record, record) + end + elsif autosave + saved = record.save(:validate => false) end - elsif autosave - saved = record.save(:validate => false) - end - raise ActiveRecord::Rollback if saved == false + raise ActiveRecord::Rollback if saved == false + end + rescue + records.each {|x| IdentityMap.remove(x) } if IdentityMap.enabled? + raise end end -- cgit v1.2.3