模块:Mongoid::Association::Referenced::BelongsTo::Buildable

包含在:
Mongoid::Association::Referenced::BelongsTo
定义于:
lib/mongoid/association/referenced/belongs_to/buildable.rb

Overview

belongs_to 关联的 Builder 行为。

实例方法摘要折叠

实例方法详细信息

#构建(_base, 对象, type = nil, selected_fields = nil) ⇒ Document

此方法接受_id或对象,并使用该ID查询反向面或设置该对象。

例子:

构建文档。

relation.build(meta, attrs)

参数:

  • 基础 ( Object )

    基础对象。

  • 对象 ( Object )

    用于构建关联的对象。

  • 类型 string (默认为: nil

    关联的类型。

  • created_fields (nil) (默认为: nil

    必须为零。

返回:

  • (文档)

    单个文档。



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mongoid/association/referenced/belongs_to/buildable.rb', line 21

def 构建(_base, 对象, 类型 = nil, created_fields = nil)
  return 对象 除非 查询?(对象)

  # 处理来自 $lookup聚合的数组(即使是 belongs_to 也会返回数组)
  if 对象.is_a?(阵列)
    first = 对象.first
    案例 first
    when nil, Mongoid::文档 then return first
    when 哈希 then return 工厂.execute_from_db(klass, first, nil, created_fields,
                                                  execute_callbacks: false)
    else 提高 ArgumentError, ' 无法从数组构建belongs_to 关联 '
    end
  end

  # 使用 $unwind 处理来自 $lookup 的单个哈希
  if 对象.is_a?(哈希)
    return 工厂.execute_from_db(klass, 对象, nil, created_fields, execute_callbacks: false)
  end

  execute_query(对象, 类型)
end