1

这是生成具有嵌套密钥的响应的唯一方法吗?使用ActiveModel Serializer生成嵌套响应的最佳实践

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 

has_many是一个串行器,它本身调用另一个串行器。这是正确的最佳方式吗?

是否有其他方法可以做到这一点?这是最有语义的方式吗?

-Jeff

回答

1

这是因为在documentation指示的正确途径。

如果已经定义了序列化程序,则不需要为delivery_product_configurations指定序列化程序。你可以这样重构:

... 
attributes :id, :short_code, :rank 

has_many :delivery_product_configurations 
...