You can have multiple serializers for the same model, e.g.
class SimplePersonSerializer < ActiveModel::Serializer
attributes :id, :name
end
and
class CompletePersonSerializer < ActiveModel::Serializer
attributes :id, :name, :phone, :email
end
simple info for people in one controller:
render json: @people, each_serializer: SimplePersonSerializer
complete info for people in another:
render json: @people, each_serializer: CompletePersonSerializer
simple info for a single person:
render json: @person, serializer: SimplePersonSerializer
complete info for a single person:
render json: @person, serializer: CompletePersonSerializer
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…