Active_model_serializers: active model serializer not showing all attributes of associated model

Created on 19 May 2017  路  3Comments  路  Source: rails-api/active_model_serializers

I have an Item. It belongs_to a User. I have an ItemSerializer and a UserSerializer in app/serializers:

class ItemSerializer < ActiveModel::Serializer
  attributes :id, :photo

  belongs_to :user
end

class UserSerializer < ActiveModel::Serializer
  attributes :id, :email, :authentication_token
end

These relationships model the relationships in app/models

When I return an ActiverRecord::Relation of items as json in my controller:

def index
    respond_to do |format|
      @items = Item.where(id: params[:item_ids)
      format.html
      format.json { render json: @items, status: 200}
    end
  end

It should be returning the user attributes, including email and authentication_token, as well. But it is only returning the user id:

... "relationships":{"user":{"data":{"id":"1","type":"users"}}} ...

Why are the other user attributes not displaying?

All 3 comments

@JohnMerlino2 that's how the JSON:API spec works. I'm guessing you're on 0.10.x and using JSON:APi. Take a look at included relationships to get what you're talking about.

@JohnMerlino2 I too have the same problem.Please post the solution if you have found it.

Was this page helpful?
0 / 5 - 0 ratings