Skip to content

Commit 1a5e66b

Browse files
charlie-waspbf4
authored andcommitted
[0.10] add docs for include (#2081)
* Add docs for `include` option in the adapter
1 parent d24a786 commit 1a5e66b

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Fixes:
1010

1111
Misc:
1212

13+
- [#2081](https://github.com/rails-api/active_model_serializers/pull/2081) Documentation for `include` option in adapters. (@charlie-wasp)
14+
1315
### [v0.10.5 (2017-03-07)](https://github.com/rails-api/active_model_serializers/compare/v0.10.4...v0.10.5)
1416

1517
Breaking changes:

docs/general/adapters.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,25 @@ This adapter follows **version 1.0** of the [format specified](../jsonapi/schema
141141
}
142142
```
143143

144-
#### Included
144+
### Include option
145145

146-
It will include the associated resources in the `"included"` member
147-
when the resource names are included in the `include` option.
148-
Including nested associated resources is also supported.
146+
Which [serializer associations](https://github.com/rails-api/active_model_serializers/blob/master/docs/general/serializers.md#associations) are rendered can be specified using the `include` option. The option usage is consistent with [the include option in the JSON API spec](http://jsonapi.org/format/#fetching-includes), and is available in all adapters.
149147

148+
Example of the usage:
150149
```ruby
151150
render json: @posts, include: ['author', 'comments', 'comments.author']
152151
# or
153152
render json: @posts, include: 'author,comments,comments.author'
154153
```
155154

155+
The format of the `include` option can be either:
156+
157+
- a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes).
158+
- an Array of Symbols and Hashes.
159+
- a mix of both.
160+
161+
An empty string or an empty array will prevent rendering of any associations.
162+
156163
In addition, two types of wildcards may be used:
157164

158165
- `*` includes one level of associations.
@@ -164,11 +171,6 @@ These can be combined with other paths.
164171
render json: @posts, include: '**' # or '*' for a single layer
165172
```
166173

167-
The format of the `include` option can be either:
168-
169-
- a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes).
170-
- an Array of Symbols and Hashes.
171-
- a mix of both.
172174

173175
The following would render posts and include:
174176

@@ -182,6 +184,20 @@ It could be combined, like above, with other paths in any combination desired.
182184
render json: @posts, include: 'author.comments.**'
183185
```
184186

187+
**Note:** Wildcards are ActiveModelSerializers-specific, they are not part of the JSON API spec.
188+
189+
The default include for the JSON API adapter is no associations. The default for the JSON and Attributes adapters is all associations.
190+
191+
For the JSON API adapter associated resources will be gathered in the `"included"` member. For the JSON and Attributes
192+
adapters associated resources will be rendered among the other attributes.
193+
194+
Only for the JSON API adapter you can specify, which attributes of associated resources will be rendered. This feature
195+
is called [sparse fieldset](http://jsonapi.org/format/#fetching-sparse-fieldsets):
196+
197+
```ruby
198+
render json: @posts, include: 'comments', fields: { comments: ['content', 'created_at'] }
199+
```
200+
185201
##### Security Considerations
186202

187203
Since the included options may come from the query params (i.e. user-controller):

0 commit comments

Comments
 (0)