I have an ERM similar to this one:
-------- -------- --------
|ModelA|-----|ModelB|-----|ModelC|
-------- -------- --------
I get ModelA and its ModelBs with the following:
modela = Repo.get(ModelA,1)
modela = preload(modela, :modelb)
Now I can access ModelB
s with modela.modelb
. But how can I preload ModelC
aswell? When I print my modelA, it says, that modelc is not loaded.
You can pass a list to Repo.preload
to load multiple associations. You can even pass a keyword list to preload nested associations.
I also find it useful to load everything that I need in the query itself (those two options below are equivalent):
query = from m in ModelA, preload: [modelb: :modelc]
Repo.get(query, 1)
Repo.get(ModelA, 1) |> Repo.preload(modelb: :modelc)
You can also pass tuples with {association_name, query}
for advanced preloding - especially useful in many associations to specify ordering.
You can read more of the documentation on preloads in the Ecto.Query.preload/3
docs
Can't comment yet but you have to define the nested relationship in ModelA. See Ecto.Schema
has_one :model_c, through: [:model_b, :model_c]
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