在我的Ruby on Rails应用程序中,我有一个带有father_id属性的Idea模型。 模型定义声明了以下关联:如何在与自身关联的Ruby on Rails模型中声明关联?
class Idea < ActiveRecord::Base
belongs_to :father, :class_name => "Idea", :foreign_key => "idea_id"
has_many :children, :class_name => "Idea", :foreign_key => "father_id", :dependent => :destroy
我觉得我得到了他们是错的,因为当我使用的铁轨控制台,我可以叫一个想法的孩子,但不是它的父亲。例如:
irb(main):008:0> i = Idea.find(75)
=> #<Idea id: 75, father_id: 66>
irb(main):009:0> i.children
=> [#<Idea id: 98, father_id: 75>, #<Idea id: 99, father_id: 75>]
这意味着通过关联调用儿童可以正常工作。虽然有一个id = 66
一个想法,我清楚地确定要使用的正确的方式
irb(main):010:0> i.father
=> nil
:foreign_key模型中的链接到自己的关联 但调用的父亲返回零。会有人请小费吗?
你可以看看[祖先](https://github.com/stefankroes/ancestry)。看起来它具有你想要的功能。 – Arjan
只是看了一下。谢谢你的建议,它的确如此。我会尝试使用它,因为我真的可以使用它的一些特性(例如子树,子树到一定的深度等等)。 – Pierre