title: Eager Loading has_many though on a belongs_to relationship
body: <p>I have some models like this:</p>
  
  <pre><code>class Image &lt; ActiveRecord::Base
    has_many :tag_groups
  end
  
  class TagGroup &lt; ActiveRecord::Base
    belongs_to :image
    has_many :tag_group_members
    has_many :tags, through: :tag_group_members
  end
  
  class TagGroupMember &lt; ActiveRecord::Base
    belongs_to :tag_group
    belongs_to :tag
  end
  
  class Tag
    has_many :tag_group_members
    has_many :tag_groups, through: :tag_group_members
  end
  </code></pre>
  
  <p>I want to select all the tag groups, and their tags, for an image. The obvious solution is something like:</p>
  
  <pre><code>Image
    .includes(tag_groups: :tags)
    .find(params[:id])
  </code></pre>
  
  <p>However, iterating through that (<code>@image.tag_groups.map{|g| g.tags.map{|t| t.name}}</code>), we find that Rails is making a rather large number of SQL queries:</p>
  
  <pre><code>TagGroup Load (0.8ms)  SELECT "tag_groups".* FROM "tag_groups" WHERE "tag_groups"."image_id" IN (1)
    Tag Exists (0.3ms)  SELECT  1 AS one FROM "tags" INNER JOIN "tag_group_members" ON "tags"."id" = "tag_group_members"."tag_id" WHERE "tag_group_members"."tag_group_id" = $1 LIMIT 1  [["tag_group_id", 1]]
    Tag Load (0.3ms)  SELECT "tags".* FROM "tags" INNER JOIN "tag_group_members" ON "tags"."id" = "tag_group_members"."tag_id" WHERE "tag_group_members"."tag_group_id" = $1  [["tag_group_id", 1]]
    Tag Exists (0.3ms)  SELECT  1 AS one FROM "tags" INNER JOIN "tag_group_members" ON "tags"."id" = "tag_group_members"."tag_id" WHERE "tag_group_members"."tag_group_id" = $1 LIMIT 1  [["tag_group_id", 3]]
    Tag Load (0.3ms)  SELECT "tags".* FROM "tags" INNER JOIN "tag_group_members" ON "tags"."id" = "tag_group_members"."tag_id" WHERE "tag_group_members"."tag_group_id" = $1  [["tag_group_id", 3]]
    Tag Exists (0.6ms)  SELECT  1 AS one FROM "tags" INNER JOIN "tag_group_members" ON "tags"."id" = "tag_group_members"."tag_id" WHERE "tag_group_members"."tag_group_id" = $1 LIMIT 1  [["tag_group_id", 4]]
    Tag Load (0.6ms)  SELECT "tags".* FROM "tags" INNER JOIN "tag_group_members" ON "tags"."id" = "tag_group_members"."tag_id" WHERE "tag_group_members"."tag_group_id" = $1  [["tag_group_id", 4]]
  </code></pre>
  
  <p>Now, it seems like this could be handled fairly easily in just three queries:</p>
  
  <pre><code>SELECT images.* FROM images WHERE images.id = $1
  </code></pre>
  
  <p>And then:</p>
  
  <pre><code>SELECT tag_groups.* FROM tag_groups WHERE tag_groups.image_id = $1
  </code></pre>
  
  <p>And, finally:</p>
  
  <pre><code>SELECT tags.*, tag_groups.id AS tag_group_id FROM tags
  INNER JOIN tag_group_members ON tag_group_members.tag_id = tags.id
  INNER JOIN tag_groups ON tag_groups.id = tag_group_members.tag_group_id
  WHERE tag_groups.image_id = $1 -- alternatively, where tag_groups.id IN (results from the previous query)
  </code></pre>
  
  <p>You can then simply associate the tags to their proper tag-groups in memory, which should be much faster then querying the database for each tag group.</p>
  
  <p>Is there any way to get Rails to load this association without doing that many joins? </p>