Skip to content

Instantly share code, notes, and snippets.

@mmun
Last active January 2, 2016 00:49
Show Gist options
  • Save mmun/3646b88d4cb2f13a256b to your computer and use it in GitHub Desktop.
Save mmun/3646b88d4cb2f13a256b to your computer and use it in GitHub Desktop.
...
def index
render @blogs, shallow: true
end
...
class BlogSerializer < ActiveModel::Serializer
attributes :id, :title
has_many :posts
def posts_serializer_options
if @options[:shallow]
{ hide_comments: true }
end
end
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments
def include_comments?
!@options[:hide_comments]
end
end
class CommentSerializer < ActiveModel::Serializer
attributes :id, :body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment