新手问题 求优化部门树形结构生成的代码的建议

hastings · January 20, 2015 · Last by 317583395 replied at January 26, 2015 · 2501 hits

有一张部门表,有 parent_id(上次部门的 id) 定义

has_many :children

按关系一次查询出所有的部门

Department.all.includes(children:[children:[children:[children:[children:[children:[children:[:children]]]]]]])

这段代码看一次揪心一次,太粗暴了

如果我已经知道部门有多深(depth),如何生成.includes(arg) 中的 arg

我的部门深度(depth)是每次部门的 parent_id 有变化的时候进行联动修改(修改自己和自己的 children),depth 的生成有什么更好的方法吗

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