From e3ff0228cd26d9287bd212206995ce0f77761e33 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sun, 15 Sep 2024 16:16:01 +0100 Subject: [PATCH] Load artists when showing a record --- lib/music_library/records.ex | 10 +++++++++- lib/music_library_web/live/record_live/show.html.heex | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 77f423f4..3ae23844 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -37,7 +37,15 @@ defmodule MusicLibrary.Records do ** (Ecto.NoResultsError) """ - def get_record!(id), do: Repo.get!(Record, id) + def get_record!(id) do + q = + from r in Record, + left_join: a in assoc(r, :artists), + preload: [artists: a], + where: r.id == ^id + + Repo.one!(q) + end @doc """ Creates a record. diff --git a/lib/music_library_web/live/record_live/show.html.heex b/lib/music_library_web/live/record_live/show.html.heex index e33507b7..c4ecd17e 100644 --- a/lib/music_library_web/live/record_live/show.html.heex +++ b/lib/music_library_web/live/record_live/show.html.heex @@ -10,6 +10,7 @@ <.list> <:item title="Type"><%= @record.type %> + <:item title="Artists"><%= Enum.map(@record.artists, fn a -> a.name end) %> <:item title="Title"><%= @record.title %> <:item title="Musicbrainz"><%= @record.musicbrainz_id %> <:item title="Year"><%= @record.year %>