다음 모델에서는 상위 작업이 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