From c10152daf948b5bdcf48cda06b9bbddee9e8c398 Mon Sep 17 00:00:00 2001 From: Anmol Chopra Date: Fri, 24 Nov 2017 17:52:35 +0530 Subject: Inverse instance should not be reloaded during autosave if called in validation Record saved in save_has_one_association already make call to association.loaded! via record's before_save callback of save_belongs_to_association, but this will reload object if accessed in record's validation. --- activerecord/lib/active_record/autosave_association.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 6974cf74f6..a1250c3835 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -436,6 +436,9 @@ module ActiveRecord if (autosave && record.changed_for_autosave?) || new_record? || record_changed?(reflection, record, key) unless reflection.through_reflection record[reflection.foreign_key] = key + if inverse_reflection = reflection.inverse_of + record.association(inverse_reflection.name).loaded! + end end saved = record.save(validate: !autosave) -- cgit v1.2.3