我使用該模型添加了嵌套在另一個對象中的對象。就像這樣:在Rails中包含嵌套的json
INGRESSO模型 - >
def as_json(options=nil)
super(:include => [:usuario, :tipo_de_ingresso])
end
在tipo_de_ingresso
模式,我要添加嵌套的另一個對象。在這裏:
def as_json(options=nil)
super(:include => :entradas)
end
但是當我拿到的ingressos.json
,我失去了entradas
。如果我得到tipo_de_ingressos.json
,entradas
嵌套,好的,但是當我得到ingressos.json
,他們不在那裏。
當我撥打ingresso
時,如何獲得嵌套在tipo_de_ingresso
中的entradas
?
PS:您可能會發現[Rabl的寶石(https://github.com/nesquena/ rabl)有用,如果你需要做更復雜的json操作。這樣你可以爲你的json響應定義實際的視圖,而不是將它捆綁在模型中。 – Soup 2013-03-11 23:38:13