Skip to content

Commit

Permalink
Overload read_attribute method
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tagliala committed Nov 27, 2022
1 parent 0a1f089 commit d6baa7a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/chrono_model/time_machine/history_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ def recorded_at
ChronoModel::Conversions.string_to_utc_time attributes_before_type_cast['recorded_at']
end

# 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` because of
# `.find` overload, the new behavior may break relations where `id` is
# still the correct attribute to read
#
# Ref: ifad/chronomodel#181
def read_attribute(attr_name, &block)
return super unless attr_name.to_s == 'id' && @primary_key.to_s == 'hid'

_read_attribute('id', &block)
end

private

def with_hid_pkey
Expand Down

0 comments on commit d6baa7a

Please sign in to comment.