7

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.

ilrock
  • 573
  • 8
  • 24

3 Answers3

4

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

Yurii
  • 682
  • 4
  • 8
  • 2
    The default behavior is indeed to show just id and type, however, as per their documentation, by using the options hash with the include property, I should get the team name and the team color. Link to documentation: https://github.com/Netflix/fast_jsonapi#compound-document – ilrock Apr 21 '19 at 11:49
  • 1
    I've attached screenshot to visualize it – Yurii Apr 21 '19 at 14:01
  • 2
    also you should use `options[:include] = [:team, :'team.name', :'team.color']` in a bit other way: `options = { include: [:team], fields: { team: [:name, :color] } }` Or create separate TeamSerializer and use it: `options = { include: [:team] }` and in ShiftSerializer: `belongs_to :team, serializer: TeamSerializer` – Yurii Apr 21 '19 at 14:13
  • 1
    I followed what the documentation said (literally copy-paste) and it's not working. WIll give a go to the other way you mentioned. I already have a team serializer created that I use for the teams endpoint. Will keep you posted. Cheers! – ilrock Apr 21 '19 at 14:52
  • 1
    Tried the second approach as well and I'm still getting the same outcome. I find it pretty weird as I'm literally taking the code from the documentation they have – ilrock Apr 21 '19 at 20:17
  • Hey, sorry for late reply. Have you figure out the problem? – Yurii Apr 24 '19 at 15:45
1

If you use the options[:include] you should create a serializer for the included model, and customize what is included in the response there.

in your case if you use

ShiftSerializer.new(shifts, include: [:team]).serializable_hash

you should create a new serializer serializers/team_serializer.rb

class TeamSerializer
  include FastJsonapi::ObjectSerializer

  attributes :name, :color
end

this way your response will be

{
  data: [
    {
      id: 1,
      type: "shift",
      relationships: {
        team: {
          data: {
            id: "22",
            type: "Team"
          }
        }
      }
    }
  ],
  included: [
    id: 22,
    type: "Team",
    attributes: {
       name: "example",
       color: "red"
    }
  ]
}

and you will find the custom data of your association in the response "included"

0

If you use like this then maybe solve your problem

class Shift < ApplicationRecord
    belongs_to :team, optional:true
    accepts_nested_attributes_for :team
end 

In your ShiftSerializer.rb please write this code,

attribute :team do |object|
    object.team.as_json
end

And you will get custom data that you want.

Reference: https://github.com/Netflix/fast_jsonapi/issues/160#issuecomment-379727174