Skip to content

Instantly share code, notes, and snippets.

@meltingice
Created May 23, 2017 21:19
Show Gist options
  • Save meltingice/bfb16f8578599dd9bd6ed626e1d7fd4b to your computer and use it in GitHub Desktop.
Save meltingice/bfb16f8578599dd9bd6ed626e1d7fd4b to your computer and use it in GitHub Desktop.
# We use includes(:member) to solve one of the N+1 issues by loading
# all of the members in a single query.
serialized_comments = article.
comments.
includes(:member).
arrange_serializable do |comment, children|
# This block is invoked for every node in the tree, which lets us
# build out a tree of serialized comments.
Api::V1::CommentSerializer.new(comment, children: children)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment