diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-03-23 18:54:20 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-23 18:54:20 +0900 |
commit | 430b84195113ad38cbb024ff0377f85245a589ba (patch) | |
tree | 8ad704834a581b9dc478dab41b43c27f0a774eb9 /activerecord/lib | |
parent | 5851ac69e11fadcd3b4bf052f1d963a5803a2449 (diff) | |
parent | 3da0024db472b5697d4b6f17a9e4edefdf4d6a96 (diff) | |
download | rails-430b84195113ad38cbb024ff0377f85245a589ba.tar.gz rails-430b84195113ad38cbb024ff0377f85245a589ba.tar.bz2 rails-430b84195113ad38cbb024ff0377f85245a589ba.zip |
Merge pull request #35683 from Kukunin/master
Bugfix: Fix false autosave for has_one :through association
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index d77d76cb1e..fe94662543 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -457,10 +457,16 @@ module ActiveRecord # If the record is new or it has changed, returns true. def record_changed?(reflection, record, key) record.new_record? || - (record.has_attribute?(reflection.foreign_key) && record[reflection.foreign_key] != key) || + association_foreign_key_changed?(reflection, record, key) || record.will_save_change_to_attribute?(reflection.foreign_key) end + def association_foreign_key_changed?(reflection, record, key) + return false if reflection.through_reflection? + + record.has_attribute?(reflection.foreign_key) && record[reflection.foreign_key] != key + end + # Saves the associated record if it's new or <tt>:autosave</tt> is enabled. # # In addition, it will destroy the association if it was marked for destruction. |