[email] <-> [customers_emails] <-> [customer]
email
id <-- email_id
customer_id --> id
I have %Email{email: _} as a :through association in my %Customer{}, but is there a way I can define my %Customer{} so that I can have customer.email map to customer.email.email?
Thanks for the reminder about select—I don’t use it very often and it slips from memory.
I found a solution using select_merge that seems good enough™
defmodule MyApp.Customer do
schema "customers" do
has_one(:customer_email, CustomerEmail)
has_one(:email, through: [:customer_email, :email])
timestamps()
end
end
defmodule MyApp.CustomerEmail
schema "customers_emails" do
belongs_to(:customer, Customer)
belongs_to(:email, Email)
timestamps()
end
end
defmodule MyApp.Email
schema "emails" do
field(:email, :string)
timestamps()
end
end
from(customer in Customer,
where: customer.id == ^customer_id,
join: email in assoc(customer, :email),
select_merge: %{email: email.email}
)
|> Repo.one()