title: How to filter preloaded records by query
body: <p>I am listing a list of records (Packs). </p>
  
  <p>Those Packs are linked to items through a many to many relation :</p>
  
  <ul>
  <li>pack has_many packitems</li>
  <li>item has_many packitems</li>
  <li>item has_many :packs, through: [:packitems, :pack]</li>
  <li>pack has_many :items, through: [:packitems, :item]</li>
  </ul>
  
  <p>When I am listing the packs, i'd like to preload their <strong>active</strong> items</p>
  
  <pre><code>def list_packs(params) do
    packs = Pack
      |&gt; preload_active_items()
      |&gt; Repo.all()
      |&gt; Repo.preload([:classroom, [packlanguages: :language]])
  end
  
  defp preload_active_items(query) do
    query
      |&gt; join(:left, [pack], _ in assoc(pack, :packitems))
      |&gt; join(:left, [_, pi], _ in assoc(pi, :item))
      |&gt; where([..., i], i.active == true)
      |&gt; preload([_, pi, i], [packitems: {pi, item: i}])
      # |&gt; preload([_, pi, i], [packitems: {pi, item: ^from(i, where: i.active == true)}])
  end
  </code></pre>
  
  <p>Everything is working fine (I retrieve the packs and their preloaded items), but I get the packs filtered on <strong>those having an active item</strong>.</p>
  
  <p>I tried to replace the where clause by the dashed line but the syntax is not correct. </p>
  
  <p>Any idea ?</p>