Hello.
I have this Ecto Shema:
Instance -> has_many(Faction) -> many_to_many(Profils)
So, instances has many factions, and profils can register into a faction (given an instance).
I would like, when I load instances, to preload their corresponding factions (easy) and to add, for each faction, a field that is the number of registered profil.
First question: I dont know if I need to add a field (virtual?) to the faction schema (such as “registered_profils”).
Second question: How would look like the request?
For now I have this request that works (but it only preload factions into instances and profils into factions):
query =
from instance in Instance,
left_join: factions in assoc(instance, :factions),
left_join: profils in assoc(factions, :profils),
preload: [factions: {factions, profils: profils}],
order_by: [desc: instance.id]
I also tried different queries such as:
query =
from instance in Instance,
left_join: factions in assoc(instance, :factions),
left_join: profils in assoc(factions, :profils),
preload: [factions: {factions, profils: profils}],
group_by: factions.id,
select: {factions.registered_profils, count(profils.id)},
order_by: [desc: instance.id]
But none works for now. Any idea?