ruby 参考has_many和belongs_to相同的模型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 参考has_many和belongs_to相同的模型相关的知识,希望对你有一定的参考价值。

# Checkout coreyward's answer to the question here: Creating a model that has a tree structure

# Basically you want to add a "parent_id" field to your folders table and then set up a relationship in your Folder model like this:

belongs_to :parent, :class_name => "Folder"
has_many :folders, :foreign_key => "parent_id"

# OR

belongs_to :parent, class_name: 'Campaign', touch: true
has_many :children, class_name: 'Campaign', foreign_key: 'parent_id', order: :priority, dependent: :destroy, inverse_of: :parent

以上是关于ruby 参考has_many和belongs_to相同的模型的主要内容,如果未能解决你的问题,请参考以下文章

ruby 参考文献has_many

如何在 Ruby on Rails 6 中正确应用 has_many 关系和 order by

Ruby-on-Rails:多个 has_many :通过可能吗?

ruby 透过'has_many through'协会来看

ruby on rails has_many 关系表单验证孩子

ruby Rails has_many:通过多态协会(http://goo.gl/lxmehk)