Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preloading deeply nested associations in Ecto

Tags:

elixir

ecto

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}")

?

like image 311
Joddari Avatar asked Feb 20 '19 22:02

Joddari


1 Answers

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.

Examples:

Using Repo.preload/3:

addr = 
  Address
  |> Repo.get(123)
  |> Repo.preload(street: [city: [region: :country]])

IO.inspect(addr.street.city.region.country)

Using 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)
like image 87
Bohdan Sviripa Avatar answered Nov 12 '22 13:11

Bohdan Sviripa