Search code examples
ruby-on-railsactive-model-serializers

Deserializing relationships JSON API ActiveModelSerializer


I'm using ActiveModelSerializers 0.10.2. - When I Deserializer my params, I'm unable to get my relationships to show up. I have a POST request with the payload:

{
  "data": {
    "type": "project_toolbox_talks",
    "attributes": {
      "date": "2017-12-11"
    }
  },
  "relationships": {
    "attendees": {
      "data": [
        {
          "type": "atendees",
          "id": "559ff2c9-beb6-47cd-9757-66104617403b"
        }
      ]
    },
    "projects": {
      "data": {
        "type": "projects",
        "id": "d9b28ffd-6f30-4dd0-a227-720caa9b881e"
      }
    }
  }
}

My Serializer for ProjectToolboxTalks is:

module API
  module V1
    class ProjectToolboxTalkSerializer < ActiveModel::Serializer
      attributes :date

      has_one :toolbox_talk
      has_one :superintendent
      has_one :project
      has_many :attendees
      has_many :project_toolbox_talk_users
    end
  end
end

In my controller when I call:

ActiveModelSerializers::Deserialization.jsonapi_parse!(
  params, only: [:date, :attendees, :projects]
)

the only thing that is returned is {:date=>"2017-12-11"}

Why are my attendees or projects relationships not being returned?

ActiveModelSerializers 0.10.2 + Rails 5.1.2 + Ruby 2.4.2


Solution

  • The key 'relationships' needs to be inside the first 'data'.