Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 6.0 breaks relations in historical models #181

Closed
tagliala opened this issue Oct 13, 2022 · 1 comment
Closed

Rails 6.0 breaks relations in historical models #181

tagliala opened this issue Oct 13, 2022 · 1 comment
Assignees
Labels
bug CRITICAL Critical issues

Comments

@tagliala
Copy link
Member

After Rails 6.0, chronomodel uses hid to search into relations when the historical object is associated without passing from the object itself

Country::History.find(france.history.last.id).attributes.fetch('id')
=> 1

Country::History.find(france.history.last.id).cities.to_sql
=> "SELECT \"cities\".* FROM \"cities\" WHERE \"cities\".\"country_id\" = 2"
    create_table :countries, temporal: true do |t|
      t.string :name

      t.timestamps
    end

    create_table :cities do |t|
      t.references :country
      t.string :name

      t.timestamps
    end
class Country < ApplicationRecord
  include ChronoModel::TimeMachine

  has_many :cities, dependent: :destroy

  validates :name, presence: true
end

class City < ApplicationRecord
  include ChronoModel::TimeMachine

  belongs_to :country

  validates :name, presence: true
end
france = Country.create!(name: 'France')
france.cities.create!(name: 'Paris')
france.update name: 'Francia'
Country::History.find(france.history.last.id).cities == Country::History.find(france.history.first.id).cities

Expected output

true

Actual output

false

@amedeo amedeo assigned amedeo and gridanjbf and unassigned amedeo Oct 13, 2022
tagliala added a commit that referenced this issue Oct 14, 2022
tagliala added a commit that referenced this issue Oct 14, 2022
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
tagliala added a commit that referenced this issue Oct 14, 2022
tagliala added a commit that referenced this issue Oct 14, 2022
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
@tagliala tagliala added bug CRITICAL Critical issues labels Oct 25, 2022
tagliala added a commit that referenced this issue Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug CRITICAL Critical issues
Projects
None yet
Development

No branches or pull requests

3 participants