Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the fast_jsonapi to return the attributes of the relationships

I have a rails api with a number of models that are being serialized by the fast_jsonapi gem.

This is what my models look like:

class Shift < ApplicationRecord
  belongs_to :team, optional: true
  ...
class Team < ApplicationRecord
  has_many :shifts
  ...

This is what the serializer looks like

class ShiftSerializer
  include FastJsonapi::ObjectSerializer
  ...
  belongs_to :team
  ...
end

The serialization works. However, even though I am including the compound team document:

def index
  shifts = policy_scope(Shift).includes(:team)
  options = {}
  options[:include] = [:team, :'team.name', :'team.color']
  render json: ShiftSerializer.new(shifts, options)
end

I'm still getting the object formatted like so:

...
relationships: {
  team: {
    data: {
      id: "22",
      type: "Team"
    }
  }
}

Whereas I'm expecting to get also the attributes of my team model.

like image 377
ilrock Avatar asked Apr 21 '19 10:04

ilrock


1 Answers

fast_jsonapi implements json api specification so respond includes "included" key, where serialized data for relationships placed.That's default behavior

enter image description here

like image 174
Yurii Avatar answered Oct 04 '22 14:10

Yurii