Browse files

Remove associated records from identity map if any raised an unexpect…

…ed exception.
  • Loading branch information...
1 parent 09f12a1 commit 7df61754a5e66ed01020800bcc9903d934dbbb7b @miloops miloops committed Sep 13, 2010
Showing with 19 additions and 14 deletions.
  1. +19 −14 activerecord/lib/active_record/autosave_association.rb
View
33 activerecord/lib/active_record/autosave_association.rb
@@ -320,22 +320,27 @@ def save_collection_association(reflection)
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

0 comments on commit 7df6175

Please sign in to comment.