Skip to content

Instantly share code, notes, and snippets.

@sergey-chechaev
Last active March 9, 2019 20:44
Show Gist options
  • Save sergey-chechaev/aaf46b967c39bcc461b16bc7392a23fd to your computer and use it in GitHub Desktop.
Save sergey-chechaev/aaf46b967c39bcc461b16bc7392a23fd to your computer and use it in GitHub Desktop.
def list_user_posts(user_id) do
Post
|> distinct(true)
|> where([p], p.user_id == ^user_id)
|> join(:left, [p], d in assoc(p, :user))
|> join(:left, [_p, d], co in assoc(d, :country))
|> join(:left, [_p, d], ci in assoc(d, :city))
|> join(:left, [p, _d], ph in assoc(p, :post_photos))
|> preload([_p, d, co, ci, ph], user: {d, country: co, city: ci}, post_photos: ph)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment