Bruce Lin
Bruce Lin

Reputation: 2740

How to pass parameters to ActiveModel serializer

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.

Upvotes: 76

Views: 60025

Answers (7)

jubeless
jubeless

Reputation: 371

As of 0.10 of active model serializer you can pass arbitrary options via the instance_options variable as seen here.

# posts_controller.rb
class PostsController < ApplicationController
  def dashboard
    render json: @post, user_id: 12
  end
end

# post_serializer.rb
class PostSerializer < ActiveModel::Serializer
  attributes :id, :title, :body
  def comments_by_me
    Comments.where(user_id: instance_options[:user_id], post_id: object.id)
  end
end

Upvotes: 16

Eric Norcross
Eric Norcross

Reputation: 4306

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

Upvotes: 117

wintondeshong
wintondeshong

Reputation: 1277

Using 0.9.3 you can use #serialization_options like so...

# app/serializers/paginated_form_serializer.rb
class PaginatedFormSerializer < ActiveModel::Serializer
  attributes :rows, :total_count

  def rows
    object.map { |o| FormSerializer.new(o) }
  end

  def total_count
    serialization_options[:total_count]
  end
end

# app/controllers/api/forms_controller.rb
class Api::FormsController < Api::ApiController
  def index
    forms = Form.page(params[:page_index]).per(params[:page_size])
    render json: forms, serializer: PaginatedFormSerializer, total_count: Form.count, status: :ok
  end
end

Upvotes: 14

Pushp Raj Saurabh
Pushp Raj Saurabh

Reputation: 1194

serialization_options works well with Active Model Serialization 0.9.3.

The options passed along with render command can be accessed in the serializer using their keys -> serialization_options[:key]

Upvotes: 8

Jon Gold
Jon Gold

Reputation: 1214

The @options hash was removed in 0.9; looks like an equivalent method was recently added -

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

class ModelSerializer::ActiveModel::Serializer
  def some_method
    puts serialization_options[:option_name]
  end
end

Upvotes: 47

Mohanraj
Mohanraj

Reputation: 4200

simple way is just add activities method in the event serializer and return n number of activities. That is it.

class EventSerializer < ActiveModel::Serializer

  has_many :activities

  def activities
    object.activities[0..9] # Select whatever you want
  end
end

Upvotes: -3

Logan Serman
Logan Serman

Reputation: 29860

Options passed in are available through the @options hash. So if you do:

respond_with @event, activity_count: 5

You can use @options[:activity_count] within the serializer.

Upvotes: 53

Related Questions