aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-05-22 15:38:15 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-05-22 15:38:15 -0700
commitf8c4805a8247a62594d29c5f3ef634d57d2abbf1 (patch)
treeb86133054b398f98d461df8b97a138390240d0f4 /activerecord/lib/active_record
parentb0f96d4436619b25b8024cc70a71c77bcfc12bf6 (diff)
parentc2362461cdf9615d5704d6f2942921b84b854c3c (diff)
downloadrails-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.rb48
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