How to pass parameters to ActiveModel serializer

Ruby on-RailsActive Model-Serializers

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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]

Solution 7 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBruce Xinda LinView Question on Stackoverflow
Solution 1 - Ruby on-RailsEric NorcrossView Answer on Stackoverflow
Solution 2 - Ruby on-RailsLogan SermanView Answer on Stackoverflow
Solution 3 - Ruby on-RailsJon GoldView Answer on Stackoverflow
Solution 4 - Ruby on-RailswintondeshongView Answer on Stackoverflow
Solution 5 - Ruby on-RailsjubelessView Answer on Stackoverflow
Solution 6 - Ruby on-RailsPushp Raj SaurabhView Answer on Stackoverflow
Solution 7 - Ruby on-RailsMohanrajView Answer on Stackoverflow