【发布时间】:2013-09-27 17:12:06
【问题描述】:
假设我有一个grandparent 文档,其中包含许多parents,并且每个parent 都有许多children。
在带有 Mongoid 的 Rails 中,在不循环的情况下获取特定 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 Performance.
标签: ruby-on-rails mongoid