1

私はgrapegemを使用APIgrape-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ます。私は問題が何であるかを取得していませんか?

4

1 に答える 1

1

ブドウのCHANGELOGをチェックすると、represent 関数が次のバージョン (0.4.6) で機能することがわかります。

0.4.6 (次へ) #114: 返される属性を選択する 'only' オプションを追加しました - @estevaoam。

そのため、今すぐこの機能を使いたい場合は、最新の github バージョンを使用できます。

gem 'grape-entity', github: "intridea/grape-entity", branch: "master"

于 2015-06-02T10:23:10.673 に答える