diff options
author | Santosh Wadghule <santosh.wadghule@gmail.com> | 2018-05-22 19:04:24 +0530 |
---|---|---|
committer | Santosh Wadghule <santosh.wadghule@gmail.com> | 2018-05-28 18:23:21 +0530 |
commit | d7a3f33dbd4726480fcbefc0c3c1270396f61fd2 (patch) | |
tree | c7fef63458aafa3893a8819ab3c918a0f6bc64b1 /activerecord/lib | |
parent | 2f76256127d35cfbfaadf162e4d8be2d0af4e453 (diff) | |
download | rails-d7a3f33dbd4726480fcbefc0c3c1270396f61fd2.tar.gz rails-d7a3f33dbd4726480fcbefc0c3c1270396f61fd2.tar.bz2 rails-d7a3f33dbd4726480fcbefc0c3c1270396f61fd2.zip |
Fix parent record should not get saved with duplicate children records
- Fixes #32940
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 8 |
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 |