将元数据添加到:attributes serializer(Active Model Serializers)

时间:2016-10-16 17:07:38

标签: ruby-on-rails ruby ruby-on-rails-5 active-model-serializers

有没有办法可以在each_serializer上添加非模型元数据属性,而不使用:json_api适配器?

render json: invoices, each_serializer: Invoices::ItemizedSerializer

我希望它是......

{
  data: [{..invoice 1...}, {...invoice 2...}]
  metadata: {total: 500}
}

而不是

[
  {..invoice 1...},
  {...invoice 2...}
]

1 个答案:

答案 0 :(得分:7)

您可以基于每个控制器传递自定义适配器:

render json: invoices, adapter: :json, each_serializer: Invoices::ItemizedSerializer, meta: {grand_total: grand_total}