使用以下模型,我正在寻找一种高效且直接的方式来返回所有具有0个父任务(实质上是顶级任务)的任务。我最终还是希望返回0个子任务,所以一个通用的解决方案会很棒。这可能使用现有的DataMapper功能,或者我需要定义一个方法来手动过滤结果?DataMapper根据关联计数筛选记录
class Task
include DataMapper::Resource
property :id, Serial
property :name , String, :required => true
#Any link of type parent where this task is the target, represents a parent of this task
has n, :links_to_parents, 'Task::Link', :child_key => [ :target_id ], :type => 'Parent'
#Any link of type parent where this task is the source, represents a child of this task
has n, :links_to_children, 'Task::Link', :child_key => [ :source_id ], :type => 'Parent'
has n, :parents, self,
:through => :links_to_parents,
:via => :source
has n, :children, self,
:through => :links_to_children,
:via => :target
def add_parent(parent)
parents.concat(Array(parent))
save
self
end
def add_child(child)
children.concat(Array(child))
save
self
end
class Link
include DataMapper::Resource
storage_names[:default] = 'task_links'
belongs_to :source, 'Task', :key => true
belongs_to :target, 'Task', :key => true
property :type, String
end
end
我希望能够在任务类似定义共享方法:
def self.without_parents
#Code to return collection here
end
谢谢!
谢谢。不是我希望的答案,但答案仍然=) – Joel