Last active
May 14, 2019 20:47
-
-
Save sergey-chechaev/d786e13c0ac8f7778d6fb5f7ef2d7558 to your computer and use it in GitHub Desktop.
Best practice for use preload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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