因此,在过去的几个小时里,我一直在靠墙敲打我的头部,试图获得它。另外,当我知道下面的事情名称时,我会更改问题的名称。Rails - 返回嵌套属性的所有值,而不仅仅是'指针'
第一个问题,这叫什么?从数据库返回的#<Comment:0x007fda3aaeb7c8>
。
其次,我试图返回(呈现json)包含子注释的评论。
事情是这样的:
[
{
id: 1,
title:'title',
body:'body'
},
{
"#< Comment:0x007fda3b3517f0>": {},
"#< Comment:0x007fda3b3517f0>": {},
}
]
如何返回这些评论的价值?当我把他们在控制台它显示了他们的属性和值,就像这样:
puts comments[0][1]
{#<Comment id: 17, body: "Another Reply Test", created_at: "2016-08-20 04:05:16", updated_at: "2016-08-20 04:05:16", parent_id: 13, user_id: 54>=>{}, #<Comment id: 18, body: "Another Reply Test", created_at: "2016-08-20 04:05:16", updated_at: "2016-08-20 04:05:16", parent_id: 13, user_id: 54>=>{}}
但如果我尝试对它们进行修改的话 - 像to_a或to_json - 它只是打击了(因为缺少一个更好的项)像这样:
puts comments[0][1].to_a
#<Comment:0x007fda3b1911b8>
{}
#<Comment:0x007fda3b190fd8>
{}
我使用Postgres的,和我使用closure_tree的hash_tree到的意见进行排序。
任何建议将非常感激,尤其是对第一个问题。
编辑: 的DEF索引返回的意见:
def index
if request.headers["type"] == 'music'
comments = Comment.where("song_id = ?", request.headers["id"]).hash_tree.to_a
comments.each do |comment|
puts comment[1] #shows all attributes and values
puts comment[1].to_a #blows up
puts comment[1].to_s #works
end
end
if comments
render json: {status:200, success:true, comments:comments}
else
render json: {status:404, success:false}
end
end
唉唉,好吧,谷歌搜索我的出路可能是不可能的。这只是我在Postman中得到的结果,我不相信它们是实际的字符串,因为这些投入是可行的。另外,to_s确实有用,你知道他们为什么没有完全渲染,只显示它们的指针吗?我更新问题以显示它是如何呈现的。 –