Hello, Elixir Forum,
I’m trying to build a query that joins four tables together and returns a map that looks like this:
IDEAL RESULT
%{
agent_domain: "Agent Domain",
agent_hostname: "Agent Hostname",
organization_name: "Organization Name",
site_name: "Site Name",
licenses: [
%{
...license keys and values.
},
%{
...license keys and values.
}
]
}
So far, I have this query working:
query =
from o in Organization,
join: s in Site,
join: a in Agent,
join: al in AgentLicense,
on:
o.id == s.organization_id and
s.id == a.site_id and
a.id == al.agent_id,
where: [id: ^id],
select: %{
organization_name: o.name,
site_name: s.name,
agent_domain_name: a.domain_name,
agent_hostname: a.hostname,
activation_licenses: al
}
Which returns a structure that looks like this:
ACTUAL RESULT
[
%{
license: %{
...license keys and values.
},
agent_domain: "Agent Domain",
agent_hostname: "Agent Hostname",
organization_name: "Organization Name",
site_name: "Site Name"
},
%{
license: %ActiphyPortal.Agents.AgentLicense{
...license keys and values.
},
agent_domain: "Agent Domain",
agent_hostname: "Agent Hostname",
organization_name: "Organization Name",
site_name: "Site Name"
}
]
The ACTUAL RESULT makes sense based on my query. My question is whether or not there is a way in Ecto to keep this as one query but have Ecto nest the result so that information isn’t duplicated, like the IDEAL RESULT structure above?
Thanks in advance for your help.