Rya*_*wis 0 mongodb mongomapper
鉴于以下类,我如何允许节点与其他节点(例如parent_node和child_nodes)具有多对多的关系?
class Node
include MongoMapper::Document
key :text, String
end
Run Code Online (Sandbox Code Playgroud)
孩子可以有多个父母吗?如果没有,很多/ belongs_to应该工作正常:
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
Run Code Online (Sandbox Code Playgroud)
如果节点可以有多个父...
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
Run Code Online (Sandbox Code Playgroud)
这就是你要找的东西吗?
| 归档时间: |
|
| 查看次数: |
230 次 |
| 最近记录: |