Loading associated schema together with the count of items (in many_to_many relations)?

Ok, not sure if this is the right/good/best way to implement, but seems to work as expected (for anyone interested):

def liked_posts_with_counts do
  likes_count =
    from lp in LikePost,
      group_by: lp.post_id,
      select: %{post_id: lp.post_id, count: count(lp.user_id)}

  from(j in Post,
    preload: [:tags],
    join: lp in subquery(likes_count),
    on: lp.post_id == p.id,
    select_merge: %{likes_count: lp.count}
  )
end

# Update: refactored this also:
def get_user!(id) do
  user = from u in User, preload: [
    created_posts: posts_with_counts(),  
    liked_posts: liked_posts_with_counts()
  ]
  Repo.get!(user, id)
end

Any improvement suggestions are welcome!