How can I nest an association on a previously composed and pre-loaded association?

Viewed 36

I have an ecto query that im trying to build that pipes an additional join and preload onto a composed query that already contains a preload.

  defp list_assets_qry(asset_id) do
    from a in Asset,
      join: u in assoc(a, :user),
      where: a.asset_id == ^asset_id,
      preload: [user: u]
  end

  list_assets_qry(asset_id)
  |> join(:left, [_,u], p in assoc(u, :partner))
  |> preload([_,u,p], user: {u, partner: p})
  |> Repo.all()

The above code will not give me my partner nested within :user like I expected.

So i have the association called :partner that is nested on user. Because of the logic flow and the existing api, I must call the list_assets_qry first. So my problem is that I need to preload a nested partner on a user that was previously pre-loaded.

0 Answers
Related