Skip to content

Commit 32c6bcc

Browse files
edits and rearranging logic
1 parent da55cd1 commit 32c6bcc

File tree

3 files changed

+31
-46
lines changed

3 files changed

+31
-46
lines changed

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10.
1616
- [Instrumentation](general/instrumentation.md)
1717
- [JSON API Schema](jsonapi/schema.md)
1818
- [ARCHITECTURE](ARCHITECTURE.md)
19-
- [Passing Arbitrary Options](general/passing_arbitrary_options.md)
2019

2120
## How to
2221

2322
- [How to add root key](howto/add_root_key.md)
2423
- [How to add pagination links](howto/add_pagination_links.md)
2524
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md)
2625
- [Testing ActiveModelSerializers](howto/test.md)
26+
- [Passing Arbitrary Options](how-to/passing_arbitrary_options.md)
2727

2828
## Integrations
2929

docs/general/passing_arbitrary_options.md

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[Back to Guides](../README.md)
2+
3+
# Passing Arbitrary Options To A Serializer
4+
5+
In addition to the [`serialization_scope`](../general/serializers.md#scope), any options passed to `render`
6+
that are not reserved for the [adapter](../general/rendering.md#adapter_opts)
7+
are available in the serializer as [instance_options](../general/serializers.md#instance_options).
8+
9+
For example, we could pass in a field, such as `user_id` into our serializer.
10+
11+
```ruby
12+
# posts_controller.rb
13+
class PostsController < ApplicationController
14+
def dashboard
15+
render json: @post, user_id: 12
16+
end
17+
end
18+
19+
# post_serializer.rb
20+
class PostSerializer < ActiveModel::Serializer
21+
attributes :id, :title, :body
22+
23+
def comments_by_me
24+
Comments.where(user_id: instance_options[:user_id], post_id: object.id)
25+
end
26+
end
27+
```
28+
29+
Since `user_id` isn't a reserved adapter option, we can process it via a serializer
30+
method. The option is passed via the `instance_options` hash.

0 commit comments

Comments
 (0)