How to pass parameters to ActiveModel serializer

Bruce Xinda Lin picture Bruce Xinda Lin · Apr 28, 2014 · Viewed 43.4k times · Source

I'm using active model serializer. I have a model event which has_many activities.

I want to return the event with the first n activities. I think I should pass the params n to the event serializer.

Answer

Eric Norcross picture Eric Norcross · Jun 22, 2016

In version ~> 0.10.0 you need to use @instance_options. Using @Jon Gold example from above:

# controller
def action
  render json: @model, option_name: value
end

# serializer
class ModelSerializer::ActiveModel::Serializer
  def some_method
    puts @instance_options[:option_name]
  end
end