diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-05-22 15:38:15 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-05-22 15:38:15 -0700 |
commit | f8c4805a8247a62594d29c5f3ef634d57d2abbf1 (patch) | |
tree | b86133054b398f98d461df8b97a138390240d0f4 /activerecord/lib/active_record | |
parent | b0f96d4436619b25b8024cc70a71c77bcfc12bf6 (diff) | |
parent | c2362461cdf9615d5704d6f2942921b84b854c3c (diff) | |
download | rails-f8c4805a8247a62594d29c5f3ef634d57d2abbf1.tar.gz rails-f8c4805a8247a62594d29c5f3ef634d57d2abbf1.tar.bz2 rails-f8c4805a8247a62594d29c5f3ef634d57d2abbf1.zip |
Merge pull request #10681 from jholton/3-2-stable-fix_association_auto_save
autosave_association issue that occurs when table has unique index (3.2.x backport)
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 48 |
1 files changed, 20 insertions, 28 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index e1499fc3b0..7bc103043e 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -331,41 +331,33 @@ module ActiveRecord autosave = reflection.options[:autosave] if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave) - begin - records_to_destroy = [] - - 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 - saved = association.insert_record(record, false) - else - association.insert_record(record) unless reflection.nested? - end - elsif autosave - saved = record.save(:validate => false) - end - raise ActiveRecord::Rollback unless saved - end + if autosave + records_to_destroy = records.select(&:marked_for_destruction?) + records_to_destroy.each { |record| association.proxy.destroy(record) } + records -= records_to_destroy + end + + records.each do |record| - records_to_destroy.each do |record| - association.proxy.destroy(record) + saved = true + + if autosave != false && (@new_record_before_save || record.new_record?) + if autosave + saved = association.insert_record(record, false) + else + association.insert_record(record) unless reflection.nested? + end + elsif autosave + saved = record.save(:validate => false) end - rescue - records.each {|x| IdentityMap.remove(x) } if IdentityMap.enabled? - raise - end + raise ActiveRecord::Rollback unless saved + end end # reconstruct the scope now that we know the owner's id - association.send(:reset_scope) if association.respond_to?(:reset_scope) + association.reset_scope if association.respond_to?(:reset_scope) end end |