В следующей модели я ищу эффективный и простой способ вернуть все Задачи, которые имеют 0 родительских задач (по сути, задачи верхнего уровня). В конце концов я захочу вернуть такие вещи, как 0 дочерних задач, так что общее решение было бы здорово. Возможно ли это с помощью существующих функций 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
Я бы хотел иметь возможность определить общий метод для класса Task, например:
def self.without_parents
#Code to return collection here
end
Спасибо!