新手问题 Rails 树形结构分组查询问题

return · April 09, 2018 · Last by Peter replied at April 09, 2018 · 1640 hits

非常感谢大家的关注,问题是这样的:有张 comments 表,大致结构结构如下

id  content user_id  parent_id
1    test1      1       null
2    test2      1       null
3    test3      1       2
4    test4      1       null    
5    test5      1       2            

其中 parent 为空代表没有回复,比如:记录 3、5 就是对记录 2 的回复 我现在想一次查询获取所有的数据树形结构,最好不改动现有的数据库结构 比如: 最终展示三条记录(1、2、4),其中记录 2 包含两天缩进的子记录(3、5) 大家有好的方法请多多指导,非常感谢~

You need to Sign in before reply, if you don't have an account, please Sign up first.