我有一个多态标签,想通过该标签搜索不明确的项目。
我该如何退还这颗葡萄实体?
class Tag < ActiveRecord::Base
belongs_to :taggable, polymorphic: true
end
class Article < ActiveRecord::Base
has_many :tags, as: :taggable
end
class Post < ActiveRecord::Base
has_many :tags, as: :taggable
end
module Api
module Entities
class Tag < Grape::Entity
expose :lable
expose :taggable # HELP: , using Api::Entities::<polymorphic>
end
end
我需要定义taggable
的实体以公开Swagger aka OpenAPI接口。
答案 0 :(得分:0)
在这种情况下,我们可以使用:
module Api
module Entities
class Tag < Grape::Entity
expose :lable
expose :taggable do |tabgable, options|
if tabgable.is_a?(Article)
API::Entities::Article.represent tabgable, options
elsif tabgable.is_a?(Post)
API::Entities::Post.represent tabgable, options
end
end
end
end
end