如何将两个模型组合在一个 RoRacts_as_tree 树视图中?
我有两个简单的模型,每个模型都有 actions_as_tree,例如“部门”和“员工”。 我的目标是创建一个树视图,将两个模型组合成一棵整体树,如下所示:
- Department 1
- 子部门1.1
- 员工 A
- 员工B
- 子部门1.2
- 子部门1.1
- 部门 2
- 子部门2.1
- 员工 C
- 子部门2.1
- 部门 3
- 子部门3.1
- 员工 D
- 员工E
- 子部门3.2
- 子部门3.1
等
我已经发现了:充当树多个模型,但恐怕我可以在正确的方向上使用更多的指针。
谢谢!
I have two simple models each with acts_as_tree, say Departments and Employees.
My goal is to create a treeview combining both models in to one overall tree, like so:
- Department 1
- SubDepartment 1.1
- Employee A
- Employee B
- SubDepartment 1.2
- SubDepartment 1.1
- Department 2
- Subdepartment 2.1
- Employee C
- Subdepartment 2.1
- Department 3
- SubDepartment 3.1
- Employee D
- Employee E
- Subdepartment 3.2
- SubDepartment 3.1
etc
I found this already: Acts as Tree with Multiple Models but I'm afraid I could use a little more pointers in the right direction.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么你的架构是这样的?
我会坚持这样做,而不是试图让树“了解”员工。唯一具有树关系的是部门。员工属于一个部门,但他们不是树形结构的一部分。
就编辑而言,当您更改部门时,您将parent_id设置为树中其父级的ID,而当您移动员工时,您将department_id设置为其“父级”的ID。
你的实际问题是什么?我的意思是你想做什么?
So your schema is like this?
I would just stick with this rather than trying to make the tree 'know' about employees. The only things that have the tree relationship are the departments. The employees belong to a department but they're not part of the tree structure.
As far as editing goes, then, when you change a department you set parent_id to be the id of its parent in the tree, and when you move an employee you set department_id to be the id of its 'parent'.
What's your actual problem? I mean what are you trying to do?