grape 如何编写有多态关联的 api? 好似直接 expose 是不行的! picture.rb
class Picture
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::BaseModel
mount_uploader :image, ImageUploader
attr_accessor :uploader_secure_token
attr_accessible :image
belongs_to :imageable, :polymorphic => true
end
product.rb
class Product
has_many :pictures, :as => :imageable, :dependent => :destroy
end
entity.rb
class Product < Grape::Entity
expose :id, :name, :cover
end
class DetailProduct < Grape::Entity
expose :id, :name, :howmany, :price, :exprice, :dprice, :mprice, :inprice, :size, :use, :feature, :inventory,
:sales, :cover, :selling, :barcode, :special_offer
expose :pictures, :using => APIEntities::Picture
end
class Picture < Grape::Entity
expose :image
end
api.rb
resource :products do
get do
@products = Product.limit(20)
present @products, :with => APIEntities::DetailProduct
end
get ":id" do
@product = Product.includes(:pictures).find(params[:id])
present @product, :with => APIEntities::DetailProduct
end
end