crashtech/torque-postgresql

Smart association reusage

Opened this issue · 0 comments

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.