Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 796 Bytes

File metadata and controls

31 lines (22 loc) · 796 Bytes

Back to Guides

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

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:

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

render json: @user, fields: { users: [:access_token] }

Where users is the JSONAPI type.