Skip to content

Commit 070ac7d

Browse files
committed
Overload read_attribute method
Starting from Rails 6.0, read_attribute will use the memoized `primary_key` if it detects that the attribute name is `id`. Since the `primary key` may have been changed to `hid` in this case because of `find` overload, the new behavior may break relations where `id` is still the correct attribute to read for foreign key Fix: #181
1 parent 9d75dd8 commit 070ac7d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Diff for: lib/chrono_model/time_machine/history_model.rb

+14
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,20 @@ def recorded_at
221221
ChronoModel::Conversions.string_to_utc_time attributes_before_type_cast['recorded_at']
222222
end
223223

224+
# Starting from Rails 6.0, read_attribute will use the memoized
225+
# `primary_key` if it detects that the attribute name is `id`.
226+
#
227+
# Since the `primary key` may have been changed to `hid` in this case
228+
# because of `find` overload, the new behavior may break relations
229+
# where `id` is still the correct attribute to read for foreign key
230+
#
231+
# Ref: ifad/chronomodel#181
232+
def read_attribute(attr_name, &block)
233+
return super unless attr_name.to_s == 'id' && @primary_key.to_s == 'hid'
234+
235+
_read_attribute('id', &block)
236+
end
237+
224238
private
225239

226240
def with_hid_pkey

0 commit comments

Comments
 (0)