Skip to content

Commit

Permalink
fix(ex/detour): use with_author instead of preload
Browse files Browse the repository at this point in the history
  • Loading branch information
firestack committed Jan 31, 2025
1 parent f79fb10 commit 130dc79
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
5 changes: 2 additions & 3 deletions lib/skate/detours/db/detour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/skate/detours/detours.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions lib/skate_web/controllers/detours_admin_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down

0 comments on commit 130dc79

Please sign in to comment.