Skip to content

Commit e6890d3

Browse files
Ten Merryclaude
authored andcommitted
fix: preload associations in non-bang get_post_by_slug to prevent KeyError
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a5c6c7b commit e6890d3

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

lib/blockster_v2/blog.ex

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,19 @@ defmodule BlocksterV2.Blog do
585585
end
586586

587587
@doc """
588-
Gets a single post by slug. Returns nil if not found.
589-
Lightweight query — no preloads.
588+
Gets a single post by slug with all associations loaded.
589+
Returns nil if not found.
590590
"""
591591
def get_post_by_slug(slug) do
592-
Repo.get_by(Post, slug: slug)
592+
from(p in Post,
593+
where: p.slug == ^slug,
594+
preload: [:author, :category, :hub, tags: ^from(t in Tag, order_by: t.name)]
595+
)
596+
|> Repo.one()
597+
|> case do
598+
nil -> nil
599+
post -> populate_author_names(post)
600+
end
593601
end
594602

595603
@doc """

0 commit comments

Comments
 (0)