更新时间:2023-11-22 17:00:52
这是可行的,但我强烈建议使用 has_many :through
代替:
This is doable, but I strongly recommend using has_many :through
instead:
class Node < ActiveRecord::Base
has_many :parent_node_links,
:class_name => 'NodeLink',
:foreign_key => :child_id
has_many :parents,
:through => :parent_node_links,
:source => :parent
has_many :child_node_links,
:class_name => 'NodeLink',
:foreign_key => :parent_id
has_many :children,
:through => :child_node_links,
:source => :child
end
class NodeLink < ActiveRecord::Base
belongs_to :parent,
:class_name => "Node"
belongs_to :child,
:class_name => "Node"
end
拥有一流的连接模型可以更轻松地管理关系,并让您可以在以后***添加相关元数据.
Having a first-class join model makes it much easier to manage the relationships and gives you the freedom to add relevant meta-data at a later point in time.