当我尝试返回嵌套注释时,我收到一个指针,例如:
def index
comments = Comment.limit(2)
newcomments = []
newcomments << comments[0]
newcomments[0].children << comments[1]
render json: {comments:newcomments}
end
我希望我会回来的是:
comments:{
'title':'title'
'body':'body',
'children':[{
'title':'title',
'body':'body,
'children':[]
}]
}
但取而代之的是:
comments:{
'title':'title'
'body':'body',
'children':[#<Comment:0x007fd243320718>:Object]
}
我是铁杆新手,并且在过去的几天里一直试图解决这个问题,任何建议都会非常感激。
这与此有关:https://github.com/rails-api/active_model_serializers/issues/835?
答案 0 :(得分:1)
你可以试试这个:
def index
comments = Comment.limit(2)
newcomments = []
newcomments << comments[0]
newcomments[0].children << comments[1].as_json
render json: {comments:newcomments}
end
答案 1 :(得分:0)
总的来说,问题很少,但是,上述答案在技术上是正确的。但是,对我的斗争更正确的答案是我需要做的
.as_json.merge!(:children=>[]).