2011-10-02 1 views

답변

5

자녀가 둘 이상의 부모를 가질 수 있습니까? 그렇지 않으면, 많은/belongs_to는 잘 작동합니다 :

class Node 
    include MongoMapper::Document 

    key :text, String 
    key :parent_node_ids, Array, :typecast => 'ObjectId' 

    many :parent_nodes, :in => :parent_node_ids, :class_name => 'Node' 

    # inverse of many :in is still forthcoming in MM 
    def child_nodes 
    Node.where(:parent_node_ids => self.id) 
    end 
end 

# ... or store an array of child_node_ids instead ... 

class Node 
    include MongoMapper::Document 

    key :text, String 
    key :child_node_ids, Array, :typecast => 'ObjectId' 

    many :child_nodes, :in => :child_node_ids, :class_name => 'Node' 

    # inverse of many :in is still forthcoming in MM 
    def parent_nodes 
    Node.where(:child_node_ids => self.id) 
    end 
end 

은 당신이 찾고있는 무엇인가요

class Node 
    include MongoMapper::Document 

    key :text, String 
    key :parent_node_id, ObjectId 

    belongs_to :parent_node, :class_name => 'Node' 
    many :child_nodes, :foreign_key => :parent_node_id, :class_name => 'Node' 
end 

노드가 여러 부모를 가질 수 있다면 ...?