How to combine two models in one RoR acts_as_tree treeview?
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
- SubDepartmen开发者_高级运维t 1.1
- Employee A
- Employee B
- SubDepartment 1.2
- SubDepartmen开发者_高级运维t 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!
So your schema is like this?
Department
acts_as_tree #requires departments.parent_id field
has_many :employees
Employee
belongs_to :department #requires employees.department_id field
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?
精彩评论