模块:Mongoid::Association::Accessors

扩展方式:
ActiveSupport::Concern
包含在:
Mongoid::Association
定义于:
lib/mongoid/association/accessors.rb

Overview

此模块包含与通过 getter 和 setter 访问关联相关的所有行为,以及如何委托构建者以创建新关联。

实例方法摘要折叠

实例方法详细信息

# __build__ (name, 对象,association,selected_fields = nil) ⇒代理

构建相关文档并创建关联,除非该文档为零,然后对此文档设置关联。

例子:

建立关联。

person.__build__(:addresses, { :_id => 1 }, association)

参数:

  • 名称 ( string | Symbol )

    关联的名称。

  • 对象 ( Hash | BSON::ObjectId )

    要使用的 ID 或属性。

  • 关联 ( Mongoid::Association::Relatable )

    关联元数据。

  • created_fields 哈希 (默认为: nil

    通过 #only 检索的字段。 如果指定了selected_fields,则无法在构建的文档中访问未在其中列出的字段。

返回:



27
28
29
30
# File 'lib/mongoid/association/accessors.rb', line 27

def __build__(名称, 对象, 关联, created_fields = nil)
  关系 = create_relation(对象, 关联, created_fields)
  set_relation(名称, 关系)
end

# create_relation (对象,association,selected_fields = nil) ⇒代理

从对象和关联元数据创建关联。

例子:

创建关联。

person.create_relation(document, association)

参数:

  • 对象 ( Document | Array< Document > )

    关联目标。

  • 关联 ( Mongoid::Association::Relatable )

    关联元数据。

  • created_fields 哈希 (默认为: nil

    通过 #only 检索的字段。 如果指定了selected_fields,则在创建的关联文档中将无法访问未在其中列出的字段。

返回:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mongoid/association/accessors.rb', line 44

def create_relation(对象, 关联, created_fields = nil)
  key = @attributes[关联.inverse_type]
  类型 = key ? 关联.解析器.model_for(key) : nil
  目标 = if t = 关联.构建(self, 对象, 类型, created_fields)
    关联.create_relation(self, t)
  else
    nil
  end

  # 只需要对嵌入式关联执行此操作。 待处理的回调
  # 仅在物化文档时添加,这种情况只发生
  # 针对嵌入式关联。 中没有调用数据库
  # 构建引用关联。
  if 关联.嵌入式?
    阵列(目标). do |doc|
      doc.try(:run_pending_callbacks)
    end
  end

  目标
end

# reset_relation_criteria (name) ⇒对象

重置关联代理内的条件。 由多对多关联用于使根本的id大量保持同步。

例子:

重置关联条件。

person.reset_relation_criteria(:preferences)

参数:

  • 名称 (符号)

    关联的名称。



73
74
75
76
77
# File 'lib/mongoid/association/accessors.rb', line 73

def reset_relation_criteria(名称)
  if instance_variable_defined?(" @_ #{ name } ")
    发送(名称).reset_unloaded
  end
end

# set_relation (name, relation) ⇒代理

将提供的关联设置为具有提供名称的类上的实例变量。 用作助手,只是为了保持代码简洁。

例子:

在文档上设置代理。

person.set(:addresses, addresses)

参数:

  • 名称 ( string | Symbol )

    关联的名称。

  • 关系 (代理)

    要设立的关联。

返回:



89
90
91
# File 'lib/mongoid/association/accessors.rb', line 89

def set_relation(名称, 关系)
  instance_variable_set(" @_ #{ name } ", 关系)
end