【发布时间】:2017-05-18 22:17:17
【问题描述】:
我有一个项目。它属于一个用户。我在 app/serializers 中有一个 ItemSerializer 和一个 UserSerializer:
class ItemSerializer < ActiveModel::Serializer
attributes :id, :photo
belongs_to :user
end
class UserSerializer < ActiveModel::Serializer
attributes :id, :email, :authentication_token
end
这些关系对 app/models 中的关系进行建模
当我在控制器中以 json 形式返回 ActiverRecord::Relation 时:
def index
respond_to do |format|
@items = Item.where(id: params[:item_ids)
format.html
format.json { render json: @items, status: 200}
end
end
它还应该返回用户属性,包括电子邮件和 authentication_token。但它只返回用户 ID:
... "relationships":{"user":{"data":{"id":"1","type":"users"}}} ...
我做错了什么?
【问题讨论】:
标签: ruby-on-rails active-model-serializers