Skip to content

Smart association reusage #83

Description

@crashtech

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions