Skip to content

Instantly share code, notes, and snippets.

@sergey-chechaev
Last active May 14, 2019 20:47
Show Gist options
  • Save sergey-chechaev/d786e13c0ac8f7778d6fb5f7ef2d7558 to your computer and use it in GitHub Desktop.
Save sergey-chechaev/d786e13c0ac8f7778d6fb5f7ef2d7558 to your computer and use it in GitHub Desktop.
Best practice for use preload
def list_user_posts(user_id) do
Post
|> where([p], p.user_id == ^user_id)
|> join(
:inner,
[post],
user in User,
on: user.id == post.user_id and is_nil(user.deleted_at) == true and
is_nil(user.blocked_at) == true
)
|> preload([p, u], user: u) # generate one query
|> preload(user: [:country, :city]) # generate 2 parallel preload query
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment