You can use Ecto.Model.assoc / 2 along with the Repo functions.
To get one item:
assoc(current_user, :posts) |> Repo.get(id)
To get all messages for the user:
assoc(current_user, :posts) |> Repo.all()
You can also use this to compile queries:
eg.
defmodule Post do use Ecto.Model ... def published(query) do from p in query, where: p.published end end assoc(current_user, :posts) |> Post.published() |> Repo.all()
source share