Ecto Query : Preloading nested associations

Hello, i’m trying to preload nested associations.

So i’m trying to get a customer’s orders. So my query looks like

query =
	from c in Customer,
		where: c.id == ^id,
		preload: [:orders]

Then later, i attach remarks to every order and want to display remarks in every order, so my updated query looks like this now.

. . .
. . .
	preload: [orders: :remarks]

But my problem now is, i also want to query the admins who made the remarks in orders. Yes, this remarks table has a field that references to admins table. But looking at my code now for preloading remarks, how can i possibly preload also the admins within the remarks?

Any idea or suggestion? tia :pray::heart:

Making a guess at the association names

preload: [orders: [remarks: :admin]]
1 Like

Wow. Thank you so much sir for the very quick response :heart:. I tried it now and it worked! Thank you :pray:

For more information on what’s possible with preload, the docs have a bunch of great examples: Ecto.Query — Ecto v3.9.1

1 Like

Thank you sir :heart:

I actually did check the docs first before i decided to post my question here. Maybe i just missed some information here and thought my problem needs some complex querying. I never thought it’s this so easy :relaxed: