Ruby on rails Rails中具有完整层次结构的JSON

Ruby on rails Rails中具有完整层次结构的JSON,ruby-on-rails,json,ruby-on-rails-4,Ruby On Rails,Json,Ruby On Rails 4,我的应用程序中有一个层次结构: 环境有容器 集装箱内有货物 项具有表达式 因此,我的模型代码如下所示: class Environment < ActiveRecord::Base has_many :containers, :dependent => :destroy def as_json(options = {}) super(options.merge(include: :containers)) end end class Container

我的应用程序中有一个层次结构:

  • 环境有容器
  • 集装箱内有货物
  • 项具有表达式
因此,我的模型代码如下所示:

class Environment < ActiveRecord::Base
  has_many :containers, :dependent => :destroy

  def as_json(options = {})
    super(options.merge(include: :containers))
  end

end

class Container < ActiveRecord::Base
  has_many :items, :dependent => :destroy
  belongs_to :environment

  def as_json(options = {})
    super(options.merge(include: :items))
  end

end

class Item < ActiveRecord::Base
  has_many :expressions, :dependent => :destroy
  belongs_to :container

  def as_json(options = {})
    super(options.merge(include: :expressions))
  end

end

class Expression < ActiveRecord::Base
  belongs_to :item

def as_json(options = {})
  super()
end

end
类环境:destroy
def as_json(选项={})
super(options.merge(include::containers))
结束
结束
类容器:销毁
属于:环境
def as_json(选项={})
super(options.merge(include::items))
结束
结束
类项:destroy
属于:容器
def as_json(选项={})
super(options.merge(include::expressions))
结束
结束
类表达式
在一个记录的常规get中,我通常只需要所需记录下方的一个层次结构,这就是为什么在
as_json
中,我只向下合并一个层次结构(get环境将返回一个容器集合,但这些容器将不包含项)

我的问题:

现在我需要的是向控制器添加一个允许完整层次结构响应的方法,即
GET/environment/getFullHierarchy/3
将返回:id=3的环境及其所有容器和每个容器的所有项以及每个项的所有表达式。而不中断当前的as\U json


我对Rails有点陌生,使用Rails 4.2.6&不知道从哪里开始-有人能帮忙吗?

当然是这样的,希望你能理解

获取层次结构json的新(环境)

假设environments表有environment\u attr1、environment\u attr2列

class EnvironmentSerializer < ActiveModel::Serializer
  attributes :environment_attr1, :environment_attr2 , :containers

  # This method is called if you have defined a 
  # attribute above which is not a direct value like for
  # a rectancle serializer will have attributes length and width
  # but you can add a attribute area as a symbol and define a method
  # area which returns object.length * object.width
  def containers
     ActiveModel::ArraySerializer.new(object.containers,
           each_serializer: ContainerSerializer)
  end
end

class ContainerSerializer < ActiveModel::Serializer
   attributes :container_attr1, :container_attr2 , :items
   def items
     ActiveModel::ArraySerializer.new(object.items,
        each_serializer: ItemSerializer)
   end
end



   class ItemSerializer < ActiveModel::Serializer
   ... 
   end

   class ExpressionSerializer < ActiveModel::Serializer
   ... 
   end
class EnvironmentSerializer
试试Thank@sethi,你能解释一下我如何使用这个宝石吗?文档还不清楚,你能在我的示例中演示一下吗?这显然可以写得更好,只是一个示例你也可以用
has-many:containers,serializer:containers-serializer
has-many:items,serializer:ItemSerializer
替换
containers和
items:items。我相信这也会有同样的效果,并且会更加ActiveModelSerializer-ish。这看起来很有希望,但我如何保持我的原始行为-在调用:index时-仅在继承权下检索一级,控制器在这两个序列化程序中的外观如何?原始行为未被改变记住EnvironmentSerializer.new(environment).to_json将为您提供层次结构。