It's clear how to preload associations in Ecto 1-2 levels deep, such as post and comments to it.
I have an Address
, and Address
belongs_to a Street
, and Street
belongs_to a City
, and City
belongs_to Region, and Region
belong_to a Country
Given Address
:
addr = Repo.get(Address, 123)
|> Repo.preload(street: ?????)
how do I preload it all the way to Country
:
IO.puts("the name of country: #{addr.street.city.region.country.name}")
?
According to the docs Repo.preload/3
is similar to Ecto.Query.preload/3
and you can pass a nested list of preloads to both of them.
Repo.preload/3
:addr =
Address
|> Repo.get(123)
|> Repo.preload(street: [city: [region: :country]])
IO.inspect(addr.street.city.region.country)
Ecto.Query.preload/3
:addr =
Address
|> Ecto.Query.where(id: 123)
|> Ecto.Query.preload(street: [city: [region: :country]])
|> Repo.one()
IO.inspect(addr.street.city.region.country)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With