Rails 3、MySQL、树形结构
我需要我的一个项目使用树结构。我找到了 此 链接,并尝试使用它。我的表结构如下所示:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用“祖先”宝石——它在支持树结构方面非常出色。它将祖先列表存储在逗号分隔的字符串中,这使得查找非常高效。
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