aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-04-10 16:21:16 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-04-10 16:42:16 +0900
commitfaf07d1468af06bb3f7f5dd0776d77dd252af3b6 (patch)
tree6bdffd96373b85eb852e6b5795c5c101eed916ac /activerecord/lib
parent7cb3e8b8efb193c13281c0d6c0354bbcc91c3b8e (diff)
parent332e7601a98ebff6a7494a556c7fe97c5691f085 (diff)
downloadrails-faf07d1468af06bb3f7f5dd0776d77dd252af3b6.tar.gz
rails-faf07d1468af06bb3f7f5dd0776d77dd252af3b6.tar.bz2
rails-faf07d1468af06bb3f7f5dd0776d77dd252af3b6.zip
Merge pull request #28155 from lcreid/belongs_to
Fix "autosave: true" on belongs_to of join model causes invalid records to be saved
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/autosave_association.rb8
1 files changed, 6 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index fe94662543..50f29a81a6 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -382,10 +382,14 @@ module ActiveRecord
if association = association_instance_get(reflection.name)
autosave = reflection.options[:autosave]
+ # By saving the instance variable in a local variable,
+ # we make the whole callback re-entrant.
+ new_record_before_save = @new_record_before_save
+
# reconstruct the scope now that we know the owner's id
association.reset_scope
- if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave)
+ if records = associated_records_to_validate_or_save(association, new_record_before_save, autosave)
if autosave
records_to_destroy = records.select(&:marked_for_destruction?)
records_to_destroy.each { |record| association.destroy(record) }
@@ -397,7 +401,7 @@ module ActiveRecord
saved = true
- if autosave != false && (@new_record_before_save || record.new_record?)
+ if autosave != false && (new_record_before_save || record.new_record?)
if autosave
saved = association.insert_record(record, false)
elsif !reflection.nested?