类:Mongoid::Association::Nested::Many
- 继承:
-
对象
- 对象
- Mongoid::Association::Nested::Many
- 包括:
- 可构建
- 定义于:
- lib/mongoid/association/nested/many.rb
Overview
用于在多对n关联上执行 #accepts_nested_attributes_for 属性分配的构建器类。
实例属性摘要
属性包含在Buildable
#association 、 #attributes 、 #existing 、 #options
实例方法摘要折叠
-
# 构建 (parent, options = {}) ⇒ 数组
根据传递给宏的属性和选项构建关联。
-
#initialize (association, properties, options = {}) ⇒ Many
构造函数
为一对多关联上的嵌套属性创建新的构建器。
Buildable中包含的方法
#allow_destroy? 、 #convert_id 、 #reject? , #update_only?
构造函数详情
#initialize (association, properties, options = {}) ⇒ Many
为一对多关联上的嵌套属性创建新的构建器。
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongoid/association/nested/many.rb', line 55 def 初始化(关联, 属性, = {}) @attributes = if 属性.respond_to?(:with_indifference_access) 属性.with_indifference_access.sort do |a, b| a[0].to_i <=> b[0].to_i end else 属性 end @association = 关联 @options = @class_name = [:class_name] ? [:class_name].Constantize : 关联.klass end |
实例方法详细信息
# 构建 (parent, options = {}) ⇒数组
根据传递给宏的属性和选项构建关联。
此操作会尝试执行3操作,包括更新现有关联、用新文档替换关联或删除关联。
如果属性不是哈希或键/值对数组,则会引发参数错误。
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mongoid/association/nested/many.rb', line 28 def 构建(父项, = {}) @existing = 父项.发送(关联.名称) 提高 Errors::TooManyNestedAttributeRecords.new(现有的, [:limit]) if over_limit?(属性) 属性.每 do |attrs| if attrs.is_a?(::哈希) process_attributes(父项, attrs.with_indifference_access) elsif attrs.is_a?(阵列) && attrs.长度 > 1 && attrs[1].respond_to?(:with_indifference_access) process_attributes(父项, attrs[1].with_indifference_access) elsif attrs.is_a?(阵列) && attrs.长度.甚至? process_attributes(父项, 哈希[*attrs].with_indifference_access) else 提高 ArgumentError, " 嵌套关联 ' #{association.name} ' 的属性必须是哈希或键/值对数组。 " end end end |