Ecto.assoc
You're seeing just the function
assoc
, go back to Ecto module for more information.
Builds a query for the association in the given struct or structs.
Examples
In the example below, we get all comments associated to the given post:
post = Repo.get Post, 1
Repo.all Ecto.assoc(post, :comments)
assoc/2
can also receive a list of posts, as long as the posts are
not empty:
posts = Repo.all from p in Post, where: is_nil(p.published_at)
Repo.all Ecto.assoc(posts, :comments)
This function can also be used to dynamically load through associations by giving it a list. For example, to get all authors for all comments for the given posts, do:
posts = Repo.all from p in Post, where: is_nil(p.published_at)
Repo.all Ecto.assoc(posts, [:comments, :author])