Rails 3、MySQL、树形结构

发布于 2024-12-18 17:03:43 字数 1108 浏览 2 评论 0原文

我需要我的一个项目使用树结构。我找到了 链接,并尝试使用它。我的表结构如下所示:

id    
user_id
parent_id
lft
rgt

当我尝试创建根时,我会这样做:

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=12 的 TreeStruct。我的树形结构数据库表的内容如下:

+----+---------+-----------+------+------+---------------------+---------------------+
| id | user_id | parent_id | lft  | rgt  | created_at          | updated_at          |
+----+---------+-----------+------+------+---------------------+---------------------+
|  1 |      12 |      NULL |    1 |    2 | 2011-11-30 04:09:41 | 2011-11-30 04:09:41 |
+----+---------+-----------+------+------+---------------------+---------------------+

请问谁能帮助我,我仍然做错了什么?我找不到使用这个 gem 的正确方法...为什么需要值为 12 的 ID?我认为这是不需要的...

万次感谢您的每一个帮助!我第二天就在努力解决这个问题,但仍然找不到主要问题

I need to one of my project use a tree structure. I found this link and I tried it to use. My table structure looks this:

id    
user_id
parent_id
lft
rgt

When I try to create a root, so I will do this:

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

And then I try to add the childern:

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

But in this moment I am stil over and over again getting the error Couldn't find TreeStruct with id=12. The content of my DB table with the tree structure is here:

+----+---------+-----------+------+------+---------------------+---------------------+
| id | user_id | parent_id | lft  | rgt  | created_at          | updated_at          |
+----+---------+-----------+------+------+---------------------+---------------------+
|  1 |      12 |      NULL |    1 |    2 | 2011-11-30 04:09:41 | 2011-11-30 04:09:41 |
+----+---------+-----------+------+------+---------------------+---------------------+

Could anyone help me, please, what I am still doing wrong? I can't find on the right way to use this gem... Why is there needed ID with the value 12? I though this is not needed...

Million times thanks for every help! I am struggling with this problem already second day and still can't find the main problem

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

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

发布评论

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

评论(1

明月夜 2024-12-25 17:03:43

我建议使用“祖先”宝石——它在支持树结构方面非常出色。它将祖先列表存储在逗号分隔的字符串中,这使得查找非常高效。

http://railscasts.com/episodes/262-trees-with-ancestry

https://github.com/stefankroes/ancestry

I'd recomment to use the "ancestry" gem -- it's excellent at supporting tree structures. It stores the list of ancestors in a comma-separated string, which makes look-ups very efficient.

http://railscasts.com/episodes/262-trees-with-ancestry

https://github.com/stefankroes/ancestry

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