开发者

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

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜