Skip to content

Instantly share code, notes, and snippets.

@vasilakisfil
Last active September 2, 2016 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasilakisfil/bf0e0233cf0a05000f94bb8dcdfe510f to your computer and use it in GitHub Desktop.
Save vasilakisfil/bf0e0233cf0a05000f94bb8dcdfe510f to your computer and use it in GitHub Desktop.
How to include only links of relationships in JSONAPI (ActiveModelSerializers gem)
class Api::V1::UserSerializer < Api::V1::BaseSerializer
type :users
attributes :id, :name
has_many :microposts, serializers: Api::V1::MicropostSerializer do
link :self, '/api/v1/microposts'
end
end
class Api::V1::UsersController < Api::V1::BaseController
before_action :load_resource
def show
auth_user = authorize_with_permissions(@user)
render jsonapi: auth_user.record, serializer: Api::V1::UserSerializer, #auth_user.record returns a user AR object!
include: [], fields: {users: [:id, :name]}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment