What if I have code similar to this:
def delete_post(%Post{} = post) do
if has_comments?(post) do
{:error, :has_comments}
else
Repo.delete(post)
end
end
defp has_comments?(%Post{} = post) do
post = Repo.preload(post, :comments, force: true)
Enum.count(post.comments) > 0
end
Should the code inside the delete_post/1 function be wrapped in Repo.transaction to prevent the situation that there was a comment added between calling the has_comments? and the Repo.delete functions?
(that example is intentionally simplified, but you get the idea…
)
Thanks a lot for any input.






















