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

Smart association reusage #83

Open
crashtech opened this issue May 25, 2023 · 0 comments
Open

Smart association reusage #83

crashtech opened this issue May 25, 2023 · 0 comments
Assignees

Comments

@crashtech
Copy link
Owner

crashtech commented May 25, 2023

Rails is not smart when the subject is association shortcuts.
Something like:

class Text < ActiveRecord::Base
  belongs_to :user
  has_many :user_comments, through: :user, source: :comments
end

class User < ActiveRecord::Base
  has_many :comments
end

query = Text.includes(user: :comments).strict_loading
query.first.user_comments # Raises error

We can safely assume that: Because association :user_comments has no scope, it is the equivalent of calling user.comments, as long as the comments association was loaded without any additional scope (or even equivalent scopes).

The idea is to assign the @target and mark the association as loaded as long as it complies with the necessary requirements. Otherwise, just go ahead and load the association.

@crashtech crashtech self-assigned this May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant