How to force reload associations in Ecto
Ecto by default doesn't load associations and you have to explicitly call preload
:
id = 1
user = Repo.get(User, id) |> Repo.preload(:posts)
If you call preload
again on the same user, Ecto won't make the SQL query again because the association is already loaded:
user = user |> Repo.preload(:posts)
But in some cases you do want to reload a preloaded association then you can use force: true
:
user = user |> Repo.preload(:posts, force: true)
Tweet