类:Mongoid::Association::Nested::Many

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

Overview

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

实例属性摘要

属性包含在Buildable

#association#attributes#existing#options

实例方法摘要折叠

Buildable中包含的方法

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

构造函数详情

#initialize (association, properties, options = {}) ⇒ Many

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

例子:

初始化构建器。

Many.new(association, attributes, options)

参数:

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

    关联元数据。

  • 属性 (哈希)

    要尝试设立的属性哈希。

  • 选项 哈希 (默认为: {}

    定义的选项。



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mongoid/association/nested/many.rb', line 50

def 初始化(关联, 属性, 选项 = {})
  if 属性.respond_to?(:with_indifference_access)
    @attributes = 属性.with_indifference_access.sort do |a, b|
      a[0].to_i <=> b[0].to_i
    end
  else
    @attributes = 属性
  end
  @association = 关联
  @options = 选项
  @class_name = 选项[:class_name] ? 选项[:class_name].Constantize : 关联.klass
end

实例方法详细信息

# 构建 (parent, options = {}) ⇒数组

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

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

例子:

构建嵌套属性。

many.build(person)

参数:

  • 父项 (文档)

    关联的父文档。

  • 选项 哈希 (默认为: {}

    选项。

返回:

  • ( Array )

    属性。



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

def 构建(父项, 选项 = {})
  @existing = 父项.发送(关联.名称)
  if over_limit?(属性)
    提高 Errors::TooManyNestedAttributeRecords.new(现有的, 选项[:limit])
  end
  属性. do |attrs|
    if attrs.is_a?(::哈希)
      process_attributes(父项, attrs.with_indifference_access)
    else
      process_attributes(父项, attrs[1].with_indifference_access)
    end
  end
end