很棒的嵌套集 - 创建数据库结构错误

发布于 2024-12-18 10:37:59 字数 1718 浏览 2 评论 0原文

我使用这个插件来创建我的树结构。我的数据库模式有一个小问题 - 我不确定它是否工作正常。 我的数据库表看起来是这样的:

+----+---------+-----------+------+------+---------------------+---------------------+
| id | user_id | parent_id | lft  | rgt  | created_at          | updated_at          |
+----+---------+-----------+------+------+---------------------+---------------------+
|  1 |       2 |      NULL |    1 |    2 | 2011-11-25 18:48:14 | 2011-11-25 18:48:14 |
|  2 |       3 |         2 |    3 |   16 | 2011-11-25 18:49:07 | 2011-11-25 18:49:07 |
|  3 |       4 |         2 |    4 |    7 | 2011-11-25 18:50:56 | 2011-11-25 18:50:56 |
|  4 |       5 |         3 |    5 |    6 | 2011-11-25 18:56:37 | 2011-11-25 18:56:37 |
|  5 |       7 |         2 |    8 |    9 | 2011-11-28 08:44:11 | 2011-11-28 08:44:11 |
|  6 |       8 |         2 |   10 |   11 | 2011-11-28 10:56:26 | 2011-11-28 10:56:26 |
|  7 |       9 |         2 |   12 |   13 | 2011-11-28 16:57:54 | 2011-11-28 16:57:54 |
|  8 |      10 |         2 |   14 |   15 | 2011-11-29 14:01:45 | 2011-11-29 14:01:45 |
+----+---------+-----------+------+------+---------------------+---------------------+

我虽然根项(ID=1)会自动更新值lftrgt,但在我的情况下不会。我仍然不知道,我做错了什么。

这是我创建项目的过程:

-创建节点(根):

save_tree = TreeStruct.create!(:user_id => @user.id)

-在根项目下创建项目:

save_tree = TreeStruct.create!(:user_id => @user.id)
save_tree.move_to_child_of(params[:parent])

在我创建项目的表单中,是隐藏输入,该输入的值是(ID ) 的根项。

我的问题 - 当表中仍然有值 lft=1rgt=2 时,我做错了什么?那里的值不应该是116吗?

百万次感谢您的每一个帮助,我对这个问题真的很绝望。

I use this plugin for creating my tree structure. I have a little problem about my DB schema - I am not sure if it works well.
My DB table looks this:

+----+---------+-----------+------+------+---------------------+---------------------+
| id | user_id | parent_id | lft  | rgt  | created_at          | updated_at          |
+----+---------+-----------+------+------+---------------------+---------------------+
|  1 |       2 |      NULL |    1 |    2 | 2011-11-25 18:48:14 | 2011-11-25 18:48:14 |
|  2 |       3 |         2 |    3 |   16 | 2011-11-25 18:49:07 | 2011-11-25 18:49:07 |
|  3 |       4 |         2 |    4 |    7 | 2011-11-25 18:50:56 | 2011-11-25 18:50:56 |
|  4 |       5 |         3 |    5 |    6 | 2011-11-25 18:56:37 | 2011-11-25 18:56:37 |
|  5 |       7 |         2 |    8 |    9 | 2011-11-28 08:44:11 | 2011-11-28 08:44:11 |
|  6 |       8 |         2 |   10 |   11 | 2011-11-28 10:56:26 | 2011-11-28 10:56:26 |
|  7 |       9 |         2 |   12 |   13 | 2011-11-28 16:57:54 | 2011-11-28 16:57:54 |
|  8 |      10 |         2 |   14 |   15 | 2011-11-29 14:01:45 | 2011-11-29 14:01:45 |
+----+---------+-----------+------+------+---------------------+---------------------+

I though the root item (with ID=1) will have automatically updated the values lft and rgt, but in my case not. I still don't know, what I am doing wrong.

Here is my procedure of creating items:

-creating node (root):

save_tree = TreeStruct.create!(:user_id => @user.id)

-creating item under the root item:

save_tree = TreeStruct.create!(:user_id => @user.id)
save_tree.move_to_child_of(params[:parent])

In the form where I create the items is the hidden input and this input have the value (ID) of root item.

My problem - what I am doing wrong, when I have still the value lft=1 and rgt=2 in the table? Shouldn't be the values there 1 and 16?

Million times thanks for every help, I am really desperate from this problem.

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

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

发布评论

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

评论(1

夜无邪 2024-12-25 10:37:59

我发现创建后必须运行MyModel.rebuild!。这似乎重做了所有的 lft、rgt 值等。

您似乎还可以在具有后代的类的实例上运行重建。该文档相当不错,我使用以下备忘单解决了大部分问题:

https://github.com/collectiveidea/awesome_nested_set/wiki/Awesome-Nested-Set-Cheat-Sheet

I found that I had to run MyModel.rebuild! after creating it. This seemed to redo all of the lft, rgt values etc.

You seem to also be able to run rebuild on an instance of a class with descendants. The documentation is fairly good, I solved most of my issues using the following cheat sheet:

https://github.com/collectiveidea/awesome_nested_set/wiki/Awesome-Nested-Set-Cheat-Sheet

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