How to implement multiple different serializers for same model using ActiveModel::Serializers?

前端 未结 4 1679
不知归路
不知归路 2020-12-07 12:00

Let\'s say you\'re implementing a REST API in Rails. When serving a collection, you might want to only include a few attributes:

/people

B

4条回答
  •  鱼传尺愫
    2020-12-07 12:19

    class CompletePersonSerializer < ActiveModel::Serializer
      root :person
      attributes :id, :name, :phone, :email
    end
    

    or

    render json: @people, each_serializer: CompletePersonSerializer, root: :person
    

提交回复
热议问题