title: Eager loading a custom join with an instance variable
body: <p>Given a system that allows users to invite other users to events:</p>
  
  <pre><code>class Event
    has_many :invites
  end
  
  class User
    has_many :invites
    has_many :invited, inverse_of: :inviter, foreign_key: :inviter_id, class_name: 'Invite'
  end
  
  class Invite
    belongs_to :user
    belongs_to :event
    belongs_to :inviter, class_name: 'User'
    has_many :invited, -&gt; (invite) { where(invites: { event_id: invite.event_id }) }, through: :user, class_name: 'Invite'
  end
  </code></pre>
  
  <p>I'm generating a simple report that shows all the invites for an event. I'm trying to modify the report to also include some actions that can be done against the <code>invited</code> for the sample event:</p>
  
  <pre><code>&lt;%- event.includes(:user).each do |invite| -%&gt;
    &lt;%= invite.user.name %&gt;
    &lt;%- invite.invited.each do |other| -%&gt;
      &lt;%= link_to invite_path(other), method: :destroy do %&gt;
        ...
      &lt;% end %&gt;
    &lt;%- end -%&gt;
  &lt;%- end -%&gt;
  </code></pre>
  
  <p>This works fine but has a N+1 issue. Attempting to eager load the custom association gives:</p>
  
  <blockquote>
    <p>DEPRECATION WARNING: The association scope 'invited' is instance dependent (the scope block takes an argument). Preloading happens before the individual instances are created. This means that there is no instance being passed to the association scope. This will most likely result in broken or incorrect behavior. Joining, Preloading and eager loading of these associations is deprecated and will be removed in the future.</p>
  </blockquote>
  
  <p>Is there a way to do eager loading like this? Is it possible to give other hints to the <code>has_many</code> instead of using a instance variable?</p>