Let's assume I have a grandparent
document with many parents
, and each parent
has many children
.
What is the best way, in Rails with Mongoid, to get all of the children
for a specific grandparent
without looping?
For example, if I were to use loops, it would look something like this (rough code):
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