RandallB
RandallB

Reputation: 5565

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?

Upvotes: 0

Views: 3339

Answers (1)

Yongcheol Charles Choi
Yongcheol Charles Choi

Reputation: 110

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

Upvotes: 6

Related Questions