mardi 15 mars 2016

Using Rails ActiveModel Serializer to generate nested responses

Is this the only way to generate a response that has nested keys?

module Api
  module V20150315
    class ProductConfigurationSerializer < ActiveModel::Serializer
      cached
      delegate :cache_key, to: :object

      embed :ids, include: true

      attributes :id, :short_code, :rank

      has_many :delivery_product_configurations,
        serializer: Api::V20150315::DeliveryProductConfigurationSerializer

    end
  end
end

The has_many in a serializer which itself calls another serializer is the best way to do this right?

Are there alternative ways to do this? Is this the most semantic way?

-Jeff

Aucun commentaire:

Enregistrer un commentaire