diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index ec8deca8061be..cd9d4154b2276 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -111,7 +111,7 @@ def require_counter_update? def replace_keys(record, force: false) target_key = record ? record._read_attribute(primary_key(record.class)) : nil - if force || owner[reflection.foreign_key] != target_key + if force || owner._read_attribute(reflection.foreign_key) != target_key owner[reflection.foreign_key] = target_key end end diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb index a0d5522fa04b0..15e6ce1e9fe0f 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -19,7 +19,7 @@ def replace_keys(record, force: false) target_type = record ? record.class.polymorphic_name : nil - if force || owner[reflection.foreign_type] != target_type + if force || owner._read_attribute(reflection.foreign_type) != target_type owner[reflection.foreign_type] = target_type end end