【发布时间】:2015-11-12 11:19:03
【问题描述】:
我有一个模特Category
class Category < ActiveRecord::Base
attributes :id, :name, :order, :x, :y, :z
has_ancestry
end
在我的控制器中,我可以使用以下内容将整个树作为 JSON 获取
Category.first.subtree.arrange_serializable
但这会返回所有 DB 属性,例如 created_at 或 id
我想使用活动模型序列化程序来塑造我的输出而不丢失树结构。
class CategorySerializer < ActiveModel::Serializer
# Children is the subtree provided by ancestry
attributes :name, :x, :children
end
控制器
class CategoryController < ActionController::Base
def index
category = Category.first
render :json => category
end
end
上面的代码只会显示第一个子级别,而不是子级的子级。 任何帮助表示赞赏
【问题讨论】:
标签: ruby-on-rails ruby active-model-serializers ancestry