Sha256: 852d495337cf3930f3c4b2d3c4d0e11a80eb5fb719512d5499dd841e590d018a

Contents?: true

Size: 796 Bytes

Versions: 12

Compression:

Stored size: 796 Bytes

Contents

[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` 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.

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
active_model_serializers_custom-0.10.90 docs/general/fields.md
active_model_serializers-0.10.9 docs/general/fields.md
active_model_serializers-0.10.8 docs/general/fields.md
active_model_serializers-0.10.7 docs/general/fields.md
active_model_serializers-0.10.6 docs/general/fields.md
active_model_serializers-0.10.5 docs/general/fields.md
active_model_serializers-0.10.4 docs/general/fields.md
agi_active_model_serializers-0.10.9 docs/general/fields.md
agi_active_model_serializers-0.10.8 docs/general/fields.md
agi_active_model_serializers-0.10.7 docs/general/fields.md
active_model_serializers-0.10.3 docs/general/fields.md
active_model_serializers-0.10.2 docs/general/fields.md