Skip to content

Commit 15f6b64

Browse files
committed
Use _read_attribute instead of []
It is not only faster, it also doesn't raise an exception if the column was not fetched in the model using select.
1 parent 8c60a21 commit 15f6b64

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

activerecord/lib/active_record/associations/belongs_to_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def require_counter_update?
111111
def replace_keys(record, force: false)
112112
target_key = record ? record._read_attribute(primary_key(record.class)) : nil
113113

114-
if force || owner[reflection.foreign_key] != target_key
114+
if force || owner._read_attribute(reflection.foreign_key) != target_key
115115
owner[reflection.foreign_key] = target_key
116116
end
117117
end

activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def replace_keys(record, force: false)
1919

2020
target_type = record ? record.class.polymorphic_name : nil
2121

22-
if force || owner[reflection.foreign_type] != target_type
22+
if force || owner._read_attribute(reflection.foreign_type) != target_type
2323
owner[reflection.foreign_type] = target_type
2424
end
2525
end

0 commit comments

Comments
 (0)