Skip to content

Commit

Permalink
Add example of getting only the association as a map (#3293)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanl committed May 6, 2020
1 parent 5614c30 commit 1bb17e5
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/ecto/query/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 431,12 @@ defmodule Ecto.Query.API do
from(city in City, preload: :country,
select: map(city, [:country_id, :name, country: [:id, :population]]))
It's also possible to select a struct from one source but only a subset of
fields from one of its associations:
from(city in City, preload: :country,
select: %{city | country: map(country: [:id, :population]))
**IMPORTANT**: When filtering fields for associations, you
MUST include the foreign keys used in the relationship,
otherwise Ecto will be unable to find associated records.
Expand Down

0 comments on commit 1bb17e5

Please sign in to comment.