Ruby 中嵌套/邻接模型的排序算法
我一直在尝试找到一种好方法来做到这一点,无论是在客户端的 Javascript 中还是在服务器的最后一刻。这是一个 Rails 应用程序,但这是一个非常普遍的问题。我有一个分层模型,当前存储在嵌套集模型中。然后该模型具有:
parent_id, lft, and rgt
我想从数据库中提取一个 select 语句中的所有模型,因此给我一个简单的模型列表,然后将它们动态排序到树状层次结构中。我还没有找到一种不需要递归的干净方法来做到这一点。我确信有一个很好的算法可以解决这个问题。谢谢。
I've been trying to find a good way of doing this, either on the client side in Javascript or at the last minute within the server. This is a Rails application but it is a pretty generic question. I have a model that is hierarchical, and is currently stored in a nested set model. The model then has:
parent_id, lft, and rgt
I would like to pull out all of the models in one select statement from the database, therefore giving me a flat list of models, and then sort them on the fly into a tree hierarchy. I have not found a clean way to do this that would not require recursion. I'm sure there is a nice algorithm out there for this. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道没有递归的算法。无论如何我想分享我的站点地图视图助手:
I don't know of a algorithm without recursion. Thought i'd share my sitemap view helper anyhow: