{
"fullTitle": "university title",
"faculties": [
{
"shortTitle": "faculty 1 short title"
},
{
"shortTitle": "faculty 2 short title"
}
]
}University
|> where(slug: "slug")
|> preload(:faculties)
|> select([:id, :fullTitle, faculties: [:shortTitle]])
|> Repo.first
University
|> where(slug: "slug")
|> preload(:faculties)
|> select([u, f], %{
fullTitle: u.fullTitle,
faculties: %{
shortTitle: f.shortTitle
}
})
|> Repo.first
from(p in Post,
join: c in assoc(p, :comments),
select: {p.title, c.body},
where: p.title == "Post 1")
|> Repo.all
|> Enum.reduce(%{title: nil, comments: []}, fn ({title, comment}, %{comments: comments}) ->
%{title: title, comments: comments ++ [%{text: comment}]}
end)
%{comments: [%{text: "Comment 1"}, %{text: "Comment 2"}], title: "Post 1"}$user = App\User::find(1);
return $user->toJson();
It worked flawlessly and is very clean and obvious. Maybe we can have a similar function here (like Repo.to_json). defmodule University do@derive {Poison.Encoder, only: [:id, :name, :title]}schema "..." do...endend
from u in University,where: u.slug == "slug",join: f in assoc(u, :faculties),select: %{fullTitle: u.fullTitle, faculties: %{shortTitle: f.shortTitle}}
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/2dada250-439f-4290-92e9-c3f331100d72%40googlegroups.com.--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
University
|> where(slug: "slug")
|> preload(:faculties)
|> select([:id, :fullTitle, faculties: [:shortTitle]])
|> Repo.first
[
{
"fullTitle": "university title",
"faculties": {
"shortTitle": "faculty 1 title"
}
},
{
"fullTitle": "university title",
"faculties": {
"shortTitle": "faculty 2 title"
}
}
]
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/5dc5bd6b-7d25-4534-bf73-b9636277a51d%40googlegroups.com.
** (Ecto.QueryError) the binding used in `from` must be selected in `select` when using `preload` in query
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
I don't want to expose all from database, I only need to take selected columns and return as json.
def from_db_to_json do
University|> where(slug: "slug")|> preload(:faculties)
|> Repo.all|> Enum.map(&uni_to_json/1)end
def from_db_to_json do
select = [:id, :slug, faculties: [:id, :name]]
University|> where(slug: "slug")|> preload(:faculties)
|> select(^select)
|> Repo.all|> to_json(select)end
defp to_json(data, select) when is_list(data) do
Enum.map(data, &to_json(&1, select))
end
defp to_json(data, select) do
for field <- select, into: %{} do
case field do
{k, v} -> {k, to_json(Map.fetch!(data, k), select)} # association
k -> {k, Map.fetch!(data, k)} # regular field
end
end
end
University|> where(slug: "slug")|> preload(:faculties)
|> select([:id, :slug, faculties: [:id, :name]])
|> Repo.all|> Repo.to_jsonend
defmodule MyApp.Repo dodef to_map(query, select) doquery|> Ecto.Query.select(^select)|> Repo.all|> to_map(select)
end
defp to_map(data, select) when is_list(data) do
Enum.map(data, &to_json(&1, select))
end
defp to_map(data, select) do
for field <- select, into: %{} do
case field do
{k, v} -> {k, to_map(Map.fetch!(data, k), select)} # association
k -> {k, Map.fetch!(data, k)} # regular field
end
end
end
Now you can call it as:end
University|> where(slug: "slug")|> preload(:faculties)
|> Repo.to_map([:id, :slug, faculties: [:university_id, :shortTitle]])
def to_map(query, select) doquery|> Ecto.Query.select(^select)|> Repo.all|> to_map(select)
end