分层对象的关联,belongs_to,has_many of Own Class
我试图通过向对象添加 parent_id
属性来使对象属于同一类的其他对象,以便活动记录可以以分层方式将它们相互关联。
我知道我可以编写一个实例方法来查找并获取它们,但我想知道是否有更rails合适的方法来完成此任务,所以我可以使用活动记录来设置它协会,如果是的话怎么办?
I'm trying to make objects belong to other objects of the same class by adding a parent_id
attribute to the object so active record can associate them with each other in a hierarchical manner.
I know I can just write an instance method to do a find and get them, but I want to know if there is a more rails appropriate way to accomplish this, so can I set this up with active record associations, if so how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来这个宝石可能有帮助:
https://github.com/skyeagle/nested_set
或acts_as_tree :
https://github.com/rails/acts_as_tree
It seems that this gem may be of help :
https://github.com/skyeagle/nested_set
or acts_as_tree:
https://github.com/rails/acts_as_tree
在数据库中嵌套数据的方法有很多种。选择哪一个取决于您将如何访问这些数据,以及您将多久更改一棵树。
以下是 Rails 当前嵌套插件的列表:http://www.ruby-toolbox。 com/categories/activerecord_nesting.html 请务必仔细阅读每个 gems 文档,以便选择最适合您情况的文档。
There are a lot of ways of nesting data in databases. The one to choose manly depends on how you are going to access this data, and how often you are going to change the tree.
Here is a list of current nesting plugins for rails: http://www.ruby-toolbox.com/categories/activerecord_nesting.html Be sure to reed each of the gems documentation in order to choose the most appropriate one for your situation.