次のモデルを使用して、親タスクが 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
ありがとう!