[英]Best practice when using 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
is a serializer which itself calls another serializer. has_many
是一个序列化器,它本身调用另一个序列化器。 Is the best way to do this right?这样做的最佳方法是正确的吗?
Are there alternative ways to do this?有没有其他方法可以做到这一点? Is this the most semantic way?
这是最语义化的方式吗?
-Jeff -杰夫
This is the correct way as indicated in the documentation .这是文档中指出的正确方法。
You do not need to specify the serializer for the delivery_product_configurations
if it already has a serializer defined.如果
delivery_product_configurations
已经定义了序列化程序,则不需要为它指定序列化程序。 You can refactor thus:您可以这样重构:
...
attributes :id, :short_code, :rank
has_many :delivery_product_configurations
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.