模块:Mongoid::Association::EagerLoadable
- 定义于:
- lib/mongoid/association/eager_loadable.rb
Overview
此模块定义条件的预先加载行为。
实例方法摘要折叠
-
#ager_load ( Docs ) ⇒ Array<Mongoid::Document>
加载给定文档的关联。
-
#ager_loadable? ⇒ true | false
指示条件是否包含应预先加载的关联包含项。
-
# preload (associations, Docs ) ⇒ 对象
加载给定文档的关联。
实例方法详细信息
#ager_load ( Docs ) ⇒ Array< Mongoid::Document >
加载给定文档的关联。
25 26 27 28 29 30 31 |
# File 'lib/mongoid/association/eager_loadable.rb', line 25 def ager_load(docs) docs.点击 do |d| if ager_loadable? 预加载(条件.包含, d) end end end |
#ager_loadable? ⇒ true | false
指示条件是否包含应预先加载的关联包含项。
16 17 18 |
# File 'lib/mongoid/association/eager_loadable.rb', line 16 def ager_loadable? !条件.包含.空? end |
# preload (associations, Docs ) ⇒对象
加载给定文档的关联。 这将以递归方式完成,以加载给定文档的关联文档的关联。
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mongoid/association/eager_loadable.rb', line 40 def 预加载(关联, docs) assoc_map = 关联.GROUP_BY(和:inverse_class_name) docs_map = {} 队列 = [ klass.to_s ] # 考虑单集合继承 队列.推动(klass.root_class.to_s) if klass != klass.root_class while klass = 队列.转变 if 作为 = assoc_map.删除(klass) 作为.每 do |assoc| 队列 << assoc.class_name # 如果此类嵌套在包含树中,则仅加载文档 # 用于其上方的关联。 如果没有父关联, # 我们将包含传递给此方法的文档中的文档。 ds = docs if assoc.parent_inclusions.长度 > 0 ds = assoc.parent_inclusions.map{ |p| docs_map[p].to_a }.展平 end res = assoc.关系.Eager_Loader([assoc], ds).运行 docs_map[assoc.名称] ||= [].to_set docs_map[assoc.名称].合并(merge)(res) end end end end |