diff options
author | Jon Leighton <j@jonathanleighton.com> | 2013-05-03 05:13:14 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2013-05-03 05:13:14 -0700 |
commit | 6023a5049172e2222181881679b56c0e29034c96 (patch) | |
tree | 1e1b278105b7b5e1864a825f3a669d3b4bd4dba8 /activerecord/lib | |
parent | 84139bad77fda9899b2fa29e238730f38ca57f3c (diff) | |
parent | 483c301e0a132a6415355be2296fb932d07bd1c5 (diff) | |
download | rails-6023a5049172e2222181881679b56c0e29034c96.tar.gz rails-6023a5049172e2222181881679b56c0e29034c96.tar.bz2 rails-6023a5049172e2222181881679b56c0e29034c96.zip |
Merge pull request #10417 from jholton/fix_association_auto_save
autosave_association issue that occurs when table has unique index (resubmission)
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index b0bd78ad46..87d4daa6d9 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -335,15 +335,18 @@ module ActiveRecord autosave = reflection.options[:autosave] if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave) - records_to_destroy = [] + + if autosave + records_to_destroy = records.select(&:marked_for_destruction?) + records_to_destroy.each { |record| association.destroy(record) } + records -= records_to_destroy + end + records.each do |record| - next if record.destroyed? saved = true - if autosave && record.marked_for_destruction? - records_to_destroy << record - elsif autosave != false && (@new_record_before_save || record.new_record?) + if autosave != false && (@new_record_before_save || record.new_record?) if autosave saved = association.insert_record(record, false) else @@ -355,10 +358,6 @@ 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 |