From 130dc7904b08f68f660f89cc93a49853f3160fa7 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Fri, 31 Jan 2025 09:04:42 -0500 Subject: [PATCH] fix(ex/detour): use `with_author` instead of `preload` --- lib/skate/detours/db/detour.ex | 5 ++--- lib/skate/detours/detours.ex | 2 +- lib/skate_web/controllers/detours_admin_controller.ex | 11 ++++++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/skate/detours/db/detour.ex b/lib/skate/detours/db/detour.ex index 626d5f743..152776548 100644 --- a/lib/skate/detours/db/detour.ex +++ b/lib/skate/detours/db/detour.ex @@ -106,11 +106,10 @@ defmodule Skate.Detours.Db.Detour do order_by(query, desc: :updated_at) end - def with_author(query \\ base(), key \\ :author) do + def with_author(query \\ base()) do from([detour: d] in query, join: a in assoc(d, :author), - as: :author, - select_merge: %{^key => a} + preload: [author: a] ) end diff --git a/lib/skate/detours/detours.ex b/lib/skate/detours/detours.ex index edff3b356..6be0f0acb 100644 --- a/lib/skate/detours/detours.ex +++ b/lib/skate/detours/detours.ex @@ -27,7 +27,7 @@ defmodule Skate.Detours.Detours do def list_detours(fields) do Skate.Detours.Db.Detour.Queries.select_fields(fields) - |> preload([:author]) + |> Skate.Detours.Db.Detour.Queries.with_author() |> Skate.Detours.Db.Detour.Queries.sorted_by_last_updated() |> Repo.all() end diff --git a/lib/skate_web/controllers/detours_admin_controller.ex b/lib/skate_web/controllers/detours_admin_controller.ex index c115dcf5a..df726ade3 100644 --- a/lib/skate_web/controllers/detours_admin_controller.ex +++ b/lib/skate_web/controllers/detours_admin_controller.ex @@ -24,9 +24,14 @@ defmodule SkateWeb.DetoursAdminController do # Status column; Required for categorizing the detour :state_value, - # For some reason, without the id explicitly present, we're not able to preload the association - :author_id, - author: [:email] + # For some reason, without the primary keys explicitly present in the + # query, we're not able to preload the association. So we need the + # `User.id` and `Detour.id` explicitly in the query. + :id, + author: [ + :email, + :id + ], ] |> Detours.list_detours() |> Enum.map(fn detour ->