Add metadata to :attributes serializer (Active Model Serializers)
Asked Answered
W

1

5

Is there a way I can add a non-model metadata attribute onto an each_serializer, without using the :json_api adapter?

render json: invoices, each_serializer: Invoices::ItemizedSerializer

I want it to be...

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

instead of

[
  {..invoice 1...},
  {...invoice 2...}
]
Windage answered 16/10, 2016 at 17:7 Comment(0)
W
11

You can pass a custom adapter on a per controller basis:

render json: invoices, adapter: :json, each_serializer: Invoices::ItemizedSerializer, meta: {grand_total: grand_total}
Windage answered 16/10, 2016 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.