Sha256: 7d7ea09f3b3ba6da0c6e00767ba432047df00cfaddeb8dbe32be236f952d47ac

Contents?: true

Size: 793 Bytes

Versions: 11

Compression:

Stored size: 793 Bytes

Contents

[Back to Guides](../README.md)

# Passing Arbitrary Options To A Serializer

In addition to the [`serialization_scope`](../general/serializers.md#scope), any options passed to `render`
that are not reserved for the [adapter](../general/rendering.md#adapter_opts)
are available in the serializer as [instance_options](../general/serializers.md#instance_options).

For example, we could pass in a field, such as `user_id` into our serializer.

```ruby
# 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
```

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
active_model_serializers_custom-0.10.90 docs/howto/passing_arbitrary_options.md
active_model_serializers-0.10.9 docs/howto/passing_arbitrary_options.md
active_model_serializers-0.10.8 docs/howto/passing_arbitrary_options.md
active_model_serializers-0.10.7 docs/howto/passing_arbitrary_options.md
active_model_serializers-0.10.6 docs/howto/passing_arbitrary_options.md
active_model_serializers-0.10.5 docs/howto/passing_arbitrary_options.md
active_model_serializers-0.10.4 docs/howto/passing_arbitrary_options.md
agi_active_model_serializers-0.10.9 docs/howto/passing_arbitrary_options.md
agi_active_model_serializers-0.10.8 docs/howto/passing_arbitrary_options.md
agi_active_model_serializers-0.10.7 docs/howto/passing_arbitrary_options.md
active_model_serializers-0.10.3 docs/howto/passing_arbitrary_options.md