active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

JSONAPI not separating associations to its own relationships object in response

Open ckl1989 opened this issue 8 years ago • 4 comments

I'm building a rails app off a tutorial I found and am trying to use a JSONAPI Active Model Serializer to generate a response of that format.

In an initializer, I've put:

ActiveModelSerializers.config.adapter = :json_api

In my gemfile:

gem 'active_model_serializers', '~> 0.10.0.rc3'

I'm expecting two resource level keys, data and relationships as per the json-api specs. However, is not separating out the relationship to its own object. This is my request for /contacts.

{
  "data": [
    {
      "id": "1",
      "type": "contacts",
      "attributes": {
        "family-name": "La",
        "given-names": "ch",
        "company": {
          "id": 1,
          "name": "Lorem Inc",
          "phone": "+1 (415) 555-1234",
          "email": "[email protected]",
          "website": "www.lorem.inc",
          "address": "213 Main St. 94063 San Francisco, CA",
          "customer_id": "10001",
          "additional_info": "",
          "created_at": "2017-01-31T05:47:02.024Z",
          "updated_at": "2017-01-31T05:47:02.024Z"
        },
        "title": null,
        "phone": null,
        "email": null,
        "website": null,
        "address": null,
        "customer-id": null,
        "additional-info": null
      }
    }
  ]
}

Company is a belong_to for contacts. Here are my serializers.

class CompanySerializer < ActiveModel::Serializer
  attributes :id, :name, :phone, :email, :website, :address, :customer_id, :additional_info
end

class ContactSerializer < ActiveModel::Serializer
  attributes :id, :family_name, :given_names, :company, :title, :phone, :email, :website, :address, :customer_id, :additional_info
end

These are my models:

class Contact < ApplicationRecord
  belongs_to :company

  validates :family_name, presence: true
  validates :given_names, presence: true
end

class Company < ApplicationRecord
  validates :name, presence: true
end

Everything else is just default generated code from rails cli. I'm not sure what else I need to add here because it is my understanding the default rails behavior is to generate a response that will show everything in the serializer. I'm assuming that jsonapi adapter should separate that out for me.

What else do I need to do to get the jsonapi adapter working properly?

ckl1989 avatar Jan 31 '17 08:01 ckl1989

This looks like a cross-posting of of https://stackoverflow.com/questions/41952229/jsonapi-active-model-serializer-not-separating-model-relationships-to-its-own-o which I thought I answered there.

I'm building a rails app off a tutorial I found

What tutorial, seems out of date

you're getting

"attributes": {
        "family-name": "La",
        "given-names": "ch",
        "company": {
          "id": 1,

which is correct given that you've specified company as an attribute

class ContactSerializer < ActiveModel::Serializer
  attributes :company

if you want company to function as a relationship, you'll need to... I could tell you, but I'd like to know what you tried, first.

bf4 avatar Feb 01 '17 15:02 bf4

So sorry, this got lost in my mailbox. @bf4 thank you so much for answering! I was missing relationships in the serializer!

Thank you for taking the time with my question!

ckl1989 avatar Feb 21 '17 22:02 ckl1989

@ckl1989 what specifically were you missing in case someone else comes by with a similar problem?

bf4 avatar Feb 21 '17 22:02 bf4

I was missing a has_one :company relationship

class ContactSerializer < ActiveModel::Serializer
  attributes :id, :family_name, :given_names, :title, :phone, :email, :website, :address, :customer_id, :additional_info
  has_one :company

ckl1989 avatar Feb 21 '17 23:02 ckl1989