views:

72

answers:

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
  • Department 2
    • Subdepartment 2.1
      • Employee C
  • Department 3
    • SubDepartment 3.1
      • Employee D
      • Employee E
    • Subdepartment 3.2

etc

I found this already: http://stackoverflow.com/questions/2142285/acts-as-tree-with-multiple-models but I'm afraid I could use a little more pointers in the right direction.

Thanks!

A: 

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?

Max Williams
What I'm trying to do is create a treeview build up from Departments and Employees. Not just from Departments or Employees. So the two models have to be combined in one big tree. Indeed I do have a setup as you suggest in your answer but I don't know how to expand it two combine the two models. Thanks for the help!
Menno
So an employee can be the parent of many departments?
Max Williams