我正在使用grapegemAPI来grape-entity生成响应。
简单show/get的请求响应很好,就像只从ActiveRecord Object. 美好的
当我尝试包含has_many关系中的数据时,它会返回与该对象相关的所有数据。美好的
但是当我represent喜欢数据时
post = Post.first
data = V1::Entities::PostEntities.represent(post, only: [:id, { comments: [:id, :body] }])
data.as_json
根据文档,它应该返回如下内容:
{
id: 1,
comments: [{
id: 1,
body: 'example'
}]
}
但它返回:
{
id: 1,
comments: [{
id: 1,
user_id: 1,
body: 'example',
created_at: 'some_timestamp',
updated_at: 'also_some_timestamp',
is_deleted: 0,
}]
}
我的PostEntities包含:
module V1
module Entities
class PostEntities < Grape::Entity
expose :id
expose :comments, with: V1::Entities::CommentEntities
end
end
end
我的CommentEntities包含:
module V1
module Entities
class CommentEntities < Grape::Entity
expose :id
expose :user_id
expose :body
expose :created_at
expose :updated_at
expose :is_deleted
end
end
end
方法有问题represent。我不明白问题是什么?