def get_all_post_by_user(user_id) do
query = from u in User,
where: u.id == ^user_id,
join: p in Post, on: p.user_id == u.id,
select: %{
user_id: u.id,
username: u.username,
title: p.title,
description: p.description
}
query |> Repo.all
end
i have this query but when i try to render it, it returns multiple set of data but same User with different Post.
like this.
[
It might be. Depends on the amount of data that you query.
A join or Ecto.Query.preload might be evil as well since then ecto would have to parse multiple almost identical rows like in your example and then put them all into some “parent” struct. Repo.preload, which fires a second query, might be more efficient in that case.
Welcome! We’re happy to help. In order for that to be possible though, it’s important that you provide full and complete information showing the code you tried, and the errors you got.