File tree Expand file tree Collapse file tree 3 files changed +39
-2
lines changed Expand file tree Collapse file tree 3 files changed +39
-2
lines changed Original file line number Diff line number Diff line change 10
10
- [ #1814 ] (https://github.com/rails-api/active_model_serializers/pull/1814 ) Ensuring read_multi works with fragment cache
11
11
12
12
Misc:
13
+ - [ #1808 ] ( https://github.com/rails-api/active_model_serializers/pull/1808 ) Adds documentation for ` fields ` option. (@luizkowalski )
13
14
14
15
### [ v0.10.1 (2016-06-16)] ( https://github.com/rails-api/active_model_serializers/compare/v0.10.0...v0.10.1 )
15
16
Original file line number Diff line number Diff line change
1
+ [ Back to Guides] ( ../README.md )
2
+
3
+ # Fields
4
+
5
+ If for any reason, you need to restrict the fields returned, you should use ` fields ` option.
6
+
7
+ For example, if you have a serializer like this
8
+
9
+ ``` ruby
10
+ class UserSerializer < ActiveModel ::Serializer
11
+ attributes :access_token , :first_name , :last_name
12
+ end
13
+ ```
14
+
15
+ and in a specific controller, you want to return ` access_token ` only, ` fields ` will help you:
16
+
17
+ ``` ruby
18
+ class AnonymousController < ApplicationController
19
+ def create
20
+ render json: User .create(activation_state: ' anonymous' ), fields: [:access_token ], status: 201
21
+ end
22
+ end
23
+ ```
24
+
25
+ Note that this is only valid for the ` json ` and ` attributes ` adapter. For the ` json_api ` adapter, you would use
26
+
27
+ ``` ruby
28
+ render json: @user , fields: { users: [:access_token ] }
29
+ ```
30
+
31
+ Where ` users ` is the JSONAPI type.
Original file line number Diff line number Diff line change @@ -73,7 +73,12 @@ See [ARCHITECTURE](../ARCHITECTURE.md) for more information.
73
73
74
74
#### fields
75
75
76
- PR please :)
76
+ If you are using ` json ` or ` attributes ` adapter
77
+ ``` ruby
78
+ render json: @user , fields: [:access_token ]
79
+ ```
80
+
81
+ See [ Fields] ( fields.md ) for more information.
77
82
78
83
#### adapter
79
84
@@ -83,7 +88,7 @@ PR please :)
83
88
84
89
``` render json: posts, each_serializer: PostSerializer, key_transform: :camel_lower ```
85
90
86
- See [ Key Transforms] ( key_transforms.md ) for more informaiton .
91
+ See [ Key Transforms] ( key_transforms.md ) for more information .
87
92
88
93
#### meta
89
94
You can’t perform that action at this time.
0 commit comments