如何将两个模型组合在一个 RoRacts_as_tree 树视图中?

发布于 2024-09-04 06:26:09 字数 530 浏览 7 评论 0原文

我有两个简单的模型,每个模型都有 actions_as_tree,例如“部门”和“员工”。 我的目标是创建一个树视图,将两个模型组合成一棵整体树,如下所示:

  • Department 1
    • 子部门1.1
      • 员工 A
      • 员工B
    • 子部门1.2
  • 部门 2
    • 子部门2.1
      • 员工 C
  • 部门 3
    • 子部门3.1
      • 员工 D
      • 员工E
    • 子部门3.2

我已经发现了:充当树多个模型,但恐怕我可以在正确的方向上使用更多的指针。

谢谢!

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: Acts as Tree with Multiple Models but I'm afraid I could use a little more pointers in the right direction.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椵侞 2024-09-11 06:26:09

那么你的架构是这样的?

Department
  acts_as_tree  #requires departments.parent_id field
  has_many :employees

Employee
  belongs_to :department #requires employees.department_id field

我会坚持这样做,而不是试图让树“了解”员工。唯一具有树关系的是部门。员工属于一个部门,但他们不是树形结构的一部分。

就编辑而言,当您更改部门时,您将parent_id设置为树中其父级的ID,而当您移动员工时,您将department_id设置为其“父级”的ID。

你的实际问题是什么?我的意思是你想做什么?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文