我在我的應用程序有一個層次結構完整的層次:JSON在Rails的
- 環境具有集裝箱
- 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
在定期得到一個記錄我通常只需要一個層次低於所需的記錄,這就是爲什麼在as_json
我合併只有一個層次結構(獲取環境將返回一個容器的集合,但這些容器將不具有項目)
我的問題:
現在我需要的是一個方法添加到控制器,允許即GET /environment/getFullHierarchy/3
完整的層次響應將返回:環境ID = 3的所有容器和每一個容器中的所有它是每個項目&將所有它的表達式沒有打破目前as_json
我有點新的Rails,用Rails敲詐4.2.6 &不知道從哪裏開始 - 任何人都可以幫忙嗎?
嘗試https://github.com/rails-api/active_model_serializers – sethi
感謝@sethi,能否請您解釋一下我如何使用這個寶石?文檔沒有明確指出,你可以在我的示例中顯示這一點嗎? – yossico