diff options
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index a98cacc78c..44323ce9db 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -334,18 +334,15 @@ module ActiveRecord autosave = reflection.options[:autosave] if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave) - - if autosave - records_to_destroy = records.select(&:marked_for_destruction?) - records_to_destroy.each { |record| association.destroy(record) } - records -= records_to_destroy - end - + records_to_destroy = [] records.each do |record| + next if record.destroyed? saved = true - if autosave != false && (@new_record_before_save || record.new_record?) + if autosave && record.marked_for_destruction? + records_to_destroy << record + elsif autosave != false && (@new_record_before_save || record.new_record?) if autosave saved = association.insert_record(record, false) else @@ -357,6 +354,10 @@ module ActiveRecord raise ActiveRecord::Rollback unless saved end + + records_to_destroy.each do |record| + association.destroy(record) + end end # reconstruct the scope now that we know the owner's id |