이 많은 grandparent
문서가 있고 parent
이 각각 children
인 것으로 가정 해 봅시다.몽고 이드 - 어린이 자녀 구하기
Mongoid가있는 레일스에서 grandparent
루핑없이 children
을 모두 얻는 가장 좋은 방법은 무엇입니까? 내가 루프를 사용한다면
예를 들어,이 (거친 코드)과 같이 보일 것입니다 :
def children
children = []
parents.each do |p|
p.children.each do |c|
children << c
end
end
children.uniq
end
class Grandparent
include Mongoid::Document
has_many :parents
end
class Parent
include Mongoid::Document
belongs_to :grandparent
has_many :children
end
class Child
include Mongoid::Document
belongs_to :parent
end
'베스트'목적의 관점에서 정의되어야한다. 성능, 가독성, 모듈성 또는 구체적으로 최적화하고 있습니까? – Mustafa
@Mustafa 성능. – Baub