Another option is to abuse Rails' eager loading to determine which associations to render:
In your rails controller:
def show
@post = Post.includes(:comments).find(params[:id])
render json: @post
end
then in AMS land:
class PostSerializer < ActiveModel::Serializer
attributes :id, :title
has_many :comments, embed: :id, serializer: CommentSerializer, include: true
def include_comments?
# would include because the association is hydrated
object.association(:comments).loaded?
end
end
Probably not the cleanest solution, but it works nicely for me!