I'm launching a course about Next.js + React.jsLearn more

Rails: Pass Parameters to ActiveModel Serializer

2018-03-14 · 1 min read

In ActiveModel::Serializer version 0.10+, use @instance_options to read the parameters passed to a Active Model serializer.

Pass the parameter from an action in a controller:

class SomeController < ApplicationController
  def action
    render json: @model, your_option_name: value

Access the parameter's value inside the corresponding ActiveModel serializer:

class ModelSerializer < ActiveModel::Serializer
  def some_method
    puts @instance_options[:your_option_name]

My Tech Newsletter

Get emails from me about programming & web development. I usually send it once a month