aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/autosave_association.rb
diff options
context:
space:
mode:
authorFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-07-02 11:57:10 -0500
committerFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-07-02 11:57:10 -0500
commitb1e509ad7a8c8264544f10f4666705cd806b5408 (patch)
tree1e33f3c99457e3395218211414917b2fae48a6b5 /activerecord/lib/active_record/autosave_association.rb
parent9e0b3fc7cfba43af55377488f991348e2de24515 (diff)
downloadrails-b1e509ad7a8c8264544f10f4666705cd806b5408.tar.gz
rails-b1e509ad7a8c8264544f10f4666705cd806b5408.tar.bz2
rails-b1e509ad7a8c8264544f10f4666705cd806b5408.zip
Backport #3329 to 3-2-stable
Fix bug with autosave collection association on new record with a marked for destroy record in autosave collection. Fixes #6918.
Diffstat (limited to 'activerecord/lib/active_record/autosave_association.rb')
-rw-r--r--activerecord/lib/active_record/autosave_association.rb36
1 files changed, 21 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index c86eaba498..e1499fc3b0 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -332,25 +332,31 @@ module ActiveRecord
if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave)
begin
- records.each do |record|
- next if record.destroyed?
+ 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
- saved = true
+ raise ActiveRecord::Rollback unless saved
+ end
- if autosave && record.marked_for_destruction?
+ records_to_destroy.each do |record|
association.proxy.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
rescue
records.each {|x| IdentityMap.remove(x) } if IdentityMap.enabled?
raise