aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/autosave_association.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-05-29 06:56:46 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-05-29 06:57:55 +0900
commit56429f1e4dd6c3fd8fcdee4a7abc1eff1ea142ee (patch)
treef724b1735778654f531d58231f12783487d8a5cc /activerecord/lib/active_record/autosave_association.rb
parentcfb493a3a311110a7b5bac6e0e74c6b5ead1040b (diff)
parentd7a3f33dbd4726480fcbefc0c3c1270396f61fd2 (diff)
downloadrails-56429f1e4dd6c3fd8fcdee4a7abc1eff1ea142ee.tar.gz
rails-56429f1e4dd6c3fd8fcdee4a7abc1eff1ea142ee.tar.bz2
rails-56429f1e4dd6c3fd8fcdee4a7abc1eff1ea142ee.zip
Merge pull request #32952 from mechanicles/32940-fix
Fix parent record should not get saved with duplicate children records
Diffstat (limited to 'activerecord/lib/active_record/autosave_association.rb')
-rw-r--r--activerecord/lib/active_record/autosave_association.rb8
1 files changed, 5 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index 9575cc24c8..a405f05e0b 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -392,7 +392,7 @@ module ActiveRecord
records -= records_to_destroy
end
- records.each do |record|
+ records.each_with_index do |record, index|
next if record.destroyed?
saved = true
@@ -401,9 +401,11 @@ module ActiveRecord
if autosave
saved = association.insert_record(record, false)
elsif !reflection.nested?
- association_saved = association.insert_record(record)
if reflection.validate?
- saved = association_saved
+ valid = association_valid?(reflection, record, index)
+ saved = valid ? association.insert_record(record, false) : false
+ else
+ association.insert_record(record)
end
end
elsif autosave