75

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.

wurde
  • 2,487
  • 2
  • 20
  • 39
Bruce Lin
  • 2,700
  • 6
  • 28
  • 38

7 Answers7

117

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
Eric Norcross
  • 4,177
  • 4
  • 28
  • 53
52

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.

Logan Serman
  • 29,447
  • 27
  • 102
  • 141
  • 7
    As of AMS 0.9.0, this doesn't work. Since AMS has no documentation, no wiki, and no discussion boards, this seems to be a dead-end at this time. – Robin Daugherty Oct 11 '14 at 21:03
  • 1
    This doesn't seem to work. Where is the documentation for this? – David Nov 04 '14 at 13:51
  • On the AMS Github page. Ctrl+F "options ", it is under "render :json". – Logan Serman Nov 04 '14 at 14:05
  • 4
    It's on 0.8 branch, not on master. https://github.com/rails-api/active_model_serializers/tree/0-8-stable We really need this. https://github.com/rails-api/active_model_serializers/issues/599 – kenn Nov 06 '14 at 16:56
  • 3
    Safer to use 0.8 version if you can since it's recommended by the AMS team. `we recommend that any new projects you start use the latest 0.8.x version of this gem. This version is the most widely used, and will most closely resemble the forthcoming release.` – bigtex777 Dec 02 '14 at 19:08
  • 27
    `@options` did not seem to work for me, it turns out the hash changed to `@instance_options` instead. That did it for me. – the_critic Apr 08 '16 at 21:32
  • active_model_serializers (0.10.2) neither does work. – prograils Feb 14 '17 at 12:56
46

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
Jon Gold
  • 1,173
  • 12
  • 17
  • 4
    I have the gem version 0.9 (updated it just to be sure) but both the options and serialization_options doesn't work. This is the error undefined local variable or method `serialization_options' – ganeshran Nov 26 '14 at 13:27
  • 1
    ganeshran is right. `serialization_options` should work on 0.9 but it seems like using 0.8 and `@options` is the only way that works at the moment. – jmosesman Nov 30 '14 at 00:42
14

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
wintondeshong
  • 1,257
  • 15
  • 19
14

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
jubeless
  • 351
  • 1
  • 3
  • 5
8

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]

Pushp Raj Saurabh
  • 1,174
  • 12
  • 16
-3

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
Mohanraj
  • 4,056
  • 2
  • 22
  • 24
  • 2
    Except you have hardcoded which activities are returned. The entire point of the 'options' hash is for the API to provide different options to the consumer of an endpoint. – rmcsharry Sep 16 '16 at 15:34