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 7: loading errors with includes of STI belongs_to #142

Closed
pzac opened this issue Mar 1, 2022 · 2 comments · Fixed by #150
Closed

Rails 7: loading errors with includes of STI belongs_to #142

pzac opened this issue Mar 1, 2022 · 2 comments · Fixed by #150
Labels

Comments

@pzac
Copy link
Contributor

pzac commented Mar 1, 2022

Film.as_of(1.day.ago).first.actors.includes(:addresses) fails if addresses use STI.

@pzac pzac added the bug label Mar 1, 2022
@tagliala
Copy link
Member

tagliala commented Mar 20, 2022

It happens because of preload of belongs_to associations, when the class name is not the same as the association

associations.to_s.classify.constantize

Reduced test case:

class Movie < ApplicationRecord
  include ChronoModel::TimeMachine

  belongs_to :studio, class_name: 'MovieStudio'
end

class MovieStudio < ApplicationRecord
  has_many :movies, inverse_of: :studio
end

Movie.as_of(Time.current).includes(:studio).first

Ref: rails/rails#40776

tagliala added a commit that referenced this issue Mar 20, 2022
tagliala added a commit that referenced this issue Mar 21, 2022
tagliala added a commit that referenced this issue Mar 21, 2022
The old behavior tried to detect class names based on 

Fix: #142
tagliala added a commit that referenced this issue Mar 21, 2022
The old behavior implemented to support Rails 7 tried to detect class
names based on association symbol. This was causing issues when
the association name did not match the ruby class

This commit fixes the old behavior

Fix: #142
@tagliala
Copy link
Member

@pzac please give a try to bugfix/fix-belongs-to-preload-142 branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants