是否或可以使acts_as_tree支持急切加载?

发布于 2024-08-06 06:55:47 字数 533 浏览 6 评论 0原文

使用acts_as_tree,我希望能够通过一个SQL调用预加载一整棵树,使其完整的子层次结构完好无损。为此,我向表中添加了一个 tree_id,它贯穿该树中的所有后代。

我已经探索了acts_as_nested_set(实际上awesome_nested_set)作为一种可能性,但由于我将树移植到其他树中,我发现出于我的目的使用嵌套集执行了太多更新。与acts_as_versioned 一起,这对我所追求的设计来说是一个不可接受的复杂化。就我的目的而言,我相信acts_as_tree更适合。

我唯一的问题是获取完整的层次结构的整棵树。 ActiveRecord 的 :include 选项适用于 :children,但不适用于 :descendants。我对编写自己的手动检索和映射关联的方法感到满意。有关如何实现此目标的任何指导或示例?

从我的角度来看,我搁置使用树(支持抓取整个结构化树)的嵌套集的唯一好处是选择性抓取树的任何子部分。我对此表示同意。

我希望避免的解决方案是消除树附带的 :children 关联,并定义并手动加载在每个树节点上定义的子数组。

Using acts_as_tree I would like to be able to preload an entire tree having its complete child hierarchy intact with one SQL call. To that end, I added a tree_id to the table and it runs through all descendants in that tree.

I had explored acts_as_nested_set (actually awesome_nested_set) as a possibility but since I graft trees into other trees, I found that using the nested set for my purposes performed far too many updates. Along with acts_as_versioned this is an unacceptable complication to the design I'm after. For my purposes, I believe acts_as_tree is better suited.

My only issue is to grab a whole tree with the hierarchy intact. The :include option of ActiveRecord works with :children but not :descendants. I am content with writing my own method for retrieving and mapping the associations manually. Any guidance or examples for how to accomplish this?

From my point of view, the only benefit of nested sets that I'm putting aside to use tree (one that supports grabbing the entire structured tree) is the selective grabbing any subsection of a tree. I'm okay with that.

The solution I'm hoping to avoid is to eliminate the :children association that comes along with tree and defining and manually loading a children array defined on each tree node.

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

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

发布评论

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

评论(2

谎言月老 2024-08-13 06:55:48

我过去研究过这个问题,IIRC 我发现您可以通过将表与其自身连接n次,使用单个 SQL 查询加载已知深度的树;但是,不可能加载任意大小的完整树。因此需要嵌套集合设计。

如果您的数据集相对较小,您可以获取所有记录并在内存中重新组装树。也许这就足够了?

I've looked into this in the past and IIRC I found that you can load a tree of a known depth with a single SQL query by joining the table with itself n times; however, it's not possible to load a complete tree of arbitrary size. Hence the need for the nested set design.

If your data set is relatively small, you could fetch all the records and re-assemble the tree(s) in memory. Perhaps that would suffice?

英雄似剑 2024-08-13 06:55:48

你可以使用 index_tree Gem,它提供了一种对树的急切加载方法

 RootModel.find(1).preload_tree

详细说明在这个 博客

You can index_tree Gem, it provides an eager loading methods for trees

 RootModel.find(1).preload_tree

Detailed description is in this blog

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