类:Mongoid::Association::Nested::One

继承:
对象
  • 对象
显示全部
包括:
可构建
定义于:
lib/mongoid/association/nested/one.rb

Overview

用于在一对多关联上执行 #accepts_nested_attributes_for 属性分配的构建器类。

实例属性摘要折叠

属性包含在Buildable

#association#attributes#existing#options

实例方法摘要折叠

Buildable中包含的方法

#allow_destroy?#convert_id#reject? , #update_only?

构造函数详情

#初始化(关联、属性、选项) ⇒一个

为一对一关联上的嵌套属性创建新的构建器。

例子:

实例化构建器。

One.new(association, attributes)

参数:

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

    关联元数据。

  • 属性 (哈希)

    要尝试设立的属性哈希。

  • 选项 (哈希)

    定义的选项。



52
53
54
55
56
57
58
# File 'lib/mongoid/association/nested/one.rb', line 52

def 初始化(关联, 属性, 选项)
  @attributes = 属性.with_indifference_access
  @association = 关联
  @options = 选项
  @class_name = class_from(选项[:class_name])
  @destroy = @attributes.删除(:_destroy)
end

实例属性详细信息

#destroy对象

返回属性销毁的值。



13
14
15
# File 'lib/mongoid/association/nested/one.rb', line 13

def 销毁
  @destroy
end

实例方法详细信息

# build (parent) ⇒文档

注意:

此操作会尝试执行3操作,包括更新现有关联、用新文档替换关联或删除关联。

根据传递给宏的属性和选项构建关联。

例子:

构建1 - 1嵌套文档。

one.build(person, as: :admin)

参数:

  • 父项 (文档)

    父文档。

返回:

  • (文档)

    构建的文档。



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mongoid/association/nested/one.rb', line 28

def 构建(父项)
  return if 拒绝?(父项, 属性)
  @existing = 父项.发送(关联.名称)
  if update?
    delete_id(属性)
    现有的.assign_attributes(属性)
  elsif 替换?
    父项.发送(关联.setter, 工厂.构建(@class_name, 属性))
  elsif 删除?
    父项.发送(关联.setter, nil)
  else
    check_for_id_violation!
  end.点击 { 父项.children_may_have_changed! }
end