title: mongoid - using includes to select children objects in a 1..N referenced relationship
body: <p>I have a scenario like this:</p>
  
  <pre><code>blog.posts
  </code></pre>
  
  <p>where each post belongs to another object, say Tag (in a belongs_to, has_many relationship), so I can do:</p>
  
  <pre><code>tag.posts
  </code></pre>
  
  <p>To prevent the N+1 problem, I want to be able to do blog.posts, but also grab each tag associated with each post, so that two queries are generated, one for the posts, and one for all the tags (based on each tag_id belonging to the post).</p>
  
  <p>I noticed in mongoid documentation I can do:</p>
  
  <pre><code>Post.includes(:tag).where(:blog_id: blog.id)
  </code></pre>
  
  <p>which will get me all posts belonging to a blog, and also getting each tag associated with the post and putting in the identity map (provided it is enabled).</p>
  
  <p>The problem is, I want to do:</p>
  
  <pre><code>blog.posts
  </code></pre>
  
  <p>and somehow redefine the query to do what I want above. Is there a way to do that?</p>
  
  <p>At the moment I'm mitigating this by defining an extension:</p>
  
  <pre><code>has_many :posts do
    def with_tags 
      includes(:tag)
    end
  end
  </code></pre>
  
  <p>so that I do</p>
  
  <pre><code>blog.posts.with_tags
  </code></pre>
  
  <p>but I would prefer that</p>
  
  <pre><code>blog.posts
  </code></pre>
  
  <p>does the above by default.</p>
  
  <p>Cheers.</p>