active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

When using with :json_api adapter: the "attributes" key is not present for has_many-associated objects

Open the-bass opened this issue 10 years ago • 2 comments

When NOT specifying an adapter, it serializes an Account (that has_many Profiles) like this:

{
  :id=>1,
  :something=>"this",
  :profiles=>[
    {
      :id=>1,
      :name=>"Mike Miller"
    },
    {
      :id=>2,
      :name=>"Mike Miller"
    }
  ]
}

Note that the :name attributes are present for each Profile. Now, when specifying the :json_api adapter, it returns

{
  :data=>{
    :id=>"1",
    :type=>"accounts",
    :attributes=>{
      :something=>"this"
    },
    :relationships=>{
      :profiles=>{
        :data=>[
          {
            :id=>"1",
            :type=>"profiles"
          },
          {
            :id=>"2",
            :type=>"profiles"
          }
        ]
      }
    }
  }
}

where the :name attributes and the whole "attributes" key is not present for each Profile.

Am I doing something wrong here or is this a bug?

I created an example application for this problem: https://github.com/esBeee/serializer_test_app It is using rails 4.2.4, active_model_serializers 0.10.0.rc4 and ruby 2.1.2.

the-bass avatar Mar 17 '16 15:03 the-bass

you need to include the relationship. via the include option.

so:

render json: account, include: 'profiles'

NullVoxPopuli avatar Mar 17 '16 15:03 NullVoxPopuli

Alright, that works! Thanks @NullVoxPopuli !

the-bass avatar Mar 17 '16 16:03 the-bass