-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fields doc #1808
Fields doc #1808
Changes from all commits
d56c6c5
6e6b6f7
ac9cdbf
a870649
9915190
98a162d
a733190
7df9cf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[Back to Guides](../README.md) | ||
|
||
# Fields | ||
|
||
If for any reason, you need to restrict the fields returned, you should use `fields` option. | ||
|
||
For example, if you have a serializer like this | ||
|
||
```ruby | ||
class UserSerializer < ActiveModel::Serializer | ||
attributes :access_token, :first_name, :last_name | ||
end | ||
``` | ||
|
||
and in a specific controller, you want to return `access_token` only, `fields` will help you: | ||
|
||
```ruby | ||
class AnonymousController < ApplicationController | ||
def create | ||
render json: User.create(activation_state: 'anonymous'), fields: [:access_token], status: 201 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as for the other comment. This snippet is only valid for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. def create
if user = User.create(activation_state: 'anonymous')
# using the :json adapter
render json: user, fields: [:access_token], adapter: :json, status: :created
# using the :json_api adapter
render json: user, fields: { users: [:access_token] }, adapter: :json_api, status: :created
# using the registered jsonapi renderer
render jsonapi: user, fields: { users: [:access_token] }, status: :created @groyoh question: when I serialize a collection with JSONAPI I might have render_options[:fields] = {items: [:id, :title, :description], locations: [:name]}
render_options[:include] = [:location] but I'm not sure how this would look with the :json adapter (lazy me did not review code or check for tests) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bf4 There is no way of doing that. The attributes and json adapter only allow to specify fields for the first level. |
||
end | ||
end | ||
``` | ||
|
||
Note that this is only valid for the `json` and `attributes` adapter. For the `json_api` adapter, you would use | ||
|
||
```ruby | ||
render json: @user, fields: { users: [:access_token] } | ||
``` | ||
|
||
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. | |
|
||
#### fields | ||
|
||
PR please :) | ||
If you are using `json` or `attributes` adapter | ||
```ruby | ||
render json: @user, fields: [:access_token] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only valid for the render json: @user, fields: { users: [:access_token] } Where Could you add the missing code snippet with a brief description (stating for which adapter each snippet is valid) or add a description above your code that states that it only valid for |
||
``` | ||
|
||
See [Fields](fields.md) for more information. | ||
|
||
#### adapter | ||
|
||
|
@@ -83,7 +88,7 @@ PR please :) | |
|
||
```render json: posts, each_serializer: PostSerializer, key_transform: :camel_lower``` | ||
|
||
See [Key Transforms](key_transforms.md) for more informaiton. | ||
See [Key Transforms](key_transforms.md) for more information. | ||
|
||
#### meta | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should begin recommending
ApplicationSerializer
...