Ecto nested preloads

October 08, 2020

The many different ways to preload

Ecto has quite a few different ways to preload entities. I always find myself forgetting each of the different ways, so I'm just going to post each approach in here for a placeholder.

Assume you have a schema such as the following:

- Person
- Friends
    - Many to many relationship with Person
- Comments
    - Belongs to Person

Keyword list

This approach is the one that I always forget exists and isn't as well documented as the other approaches. It's also the main reason why I wrote this post.

App.Repo.preload(person, [friends: [], posts: [:comments]])

© 2023, Built with ❤️ by Blake Dietz