title: Preloading both sides of an Ecto.Association
body: <p>How can I preload both sides of a <code>has_many</code> and <code>belongs_to</code> association with Ecto?</p>
  
  <p>For example, in a situation that where an article <code>has_many</code> comments and comments <code>belong_to</code> one article:</p>
  
  <pre><code>defmodule MyApp.Article do
    schema "articles" do
      has_many :comments, MyApp.Comments
  
      timestamps
    end
  end
  
  defmodule MyApp.Comment do
    schema "comments" do
      belongs_to :article, MyApp.Article
  
      timestamps
    end
  end
  </code></pre>
  
  <p>How can I create a query for an Article that preloads its <code>comments</code> association and the <code>post</code> association on each Comment without performing 3 separate queries? The third one to find each of the Posts again seems unneccessary.</p>
  
  <pre><code># Does 2 queries to return the Article with `comments` preloaded but not
  # the `post` on each Comment
  Repo.get!(Article, 123) |&gt; Repo.preload(:comments)
  
  # Does 3 queries to return the Article with `comments` preloaded and
  # preloads `post` for each Comment
  Repo.get!(Article, 123) |&gt; Repo.preload([:comments, comments: :post])
  </code></pre>