Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

active model serializer not showing all attributes of associated model #2137

Closed
JohnMerlino2 opened this issue May 18, 2017 · 3 comments
Closed

Comments

@JohnMerlino2
Copy link

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?

@bf4
Copy link
Member

bf4 commented May 19, 2017

@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.

@bf4 bf4 closed this as completed May 19, 2017
@indrapranesh
Copy link

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

@saiqulhaq
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants