Q:

Active Model Serializers: How to pass options to a collection?

If I have a collection of something, say Widgets, and I'm using Active Model Serializers to serialize the collection of Widgets, how do I pass instance_options to a collection?
render json: @widgets, count: 40

I tried the above and I can't seem to get count: 40 in my instance_options. Am I missing something?

A:

You can call @instance_options[:count] in method of WidgetsSerializer.
In controller:
render json: @widgets, count: 40

For exmaple,
class WidgetsSerializer < ActiveModel::Serializer
  attributes :count

  def count
    @instance_options[:count] #=> 40
  end
end

