有没有办法可以在each_serializer
上添加非模型元数据属性,而不使用:json_api
适配器?
render json: invoices, each_serializer: Invoices::ItemizedSerializer
我希望它是......
{
data: [{..invoice 1...}, {...invoice 2...}]
metadata: {total: 500}
}
而不是
[
{..invoice 1...},
{...invoice 2...}
]
答案 0 :(得分:7)
您可以基于每个控制器传递自定义适配器:
render json: invoices, adapter: :json, each_serializer: Invoices::ItemizedSerializer, meta: {grand_total: grand_total}