Merge pull request #1545 from CodedBeardedSignedTaylor/1506-document-passing-arbitrary-options-to-serializer

[DOCS] 1506 document passing arbitrary options to serializer
This commit is contained in:
Benjamin Fleischer 2016-03-07 01:09:50 -06:00
commit 1a312bc2d3
2 changed files with 28 additions and 0 deletions

View File

@ -25,6 +25,7 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10.
- [How to add pagination links](howto/add_pagination_links.md) - [How to add pagination links](howto/add_pagination_links.md)
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md) - [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md)
- [Testing ActiveModelSerializers](howto/test.md) - [Testing ActiveModelSerializers](howto/test.md)
- [Passing Arbitrary Options](howto/passing_arbitrary_options.md)
## Integrations ## Integrations

View File

@ -0,0 +1,27 @@
[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
```