-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
end | ||
end | ||
``` | ||
|
||
Note that this is only valid for the `json` adapter. For the `json_api` adapter, you would use | ||
|
||
```ruby | ||
render json: @user, fields: { users: [:access_token] } | ||
``` | ||
|
||
Where `users` is the JSONAPI type. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters