模块:Mongoid::Attributes::Nested::ClassMethods

定义于:
lib/mongoid/attributes/nested.rb

常量摘要折叠

REJECT_ALL_BLANK_PROC =
->(属性){
  属性.全部? { |key, | key == ' _destroy ' || .空白? }
}

实例方法摘要折叠

实例方法详细信息

#accepts_nested_attributes_for (*args) ⇒对象

当需要从父关联更新相关模型时使用。 可用于嵌入式或引用关联。

例子:

定义嵌套属性。


class Person
  include Mongoid::Document

  embeds_many :addresses
  embeds_one :game
  references_many :posts

  accepts_nested_attributes_for :addresses, :game, :posts
end

参数:

  • *args 符号...哈希

    关联名称列表,后跟选项的可选哈希值。



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mongoid/attributes/nested.rb', line 48

def accept_nested_attributes_for(*args)
  选项 = args.extract_options!.dup
  选项[:autosave] = true if 选项[:autosave].nil?

  选项[:reject_if] = REJECT_ALL_BLANK_PROC if 选项[:reject_if] == :all_blank
  args. do |名称|
    meth = " #{ name } _attributes= "
    self.nested_attributes[" #{ name } _attributes "] = meth
    关联 = 关系[名称.to_s]
    提高 Errors::NestedAttributesMetadataNotFound.new(self, 名称) 除非 关联
    autosave_nested_attributes(关联) if 选项[:autosave]

    re_define_method(meth) do |attrs|
      _assing do
        if 关联.多态?  关联.inverse_type
          klass = 关联.解析器.model_for(发送(关联.inverse_type))
          选项 = 选项.合并!(:class_name => klass)
        end
        关联.nested_builder(attrs, 选项).构建(self)
      end
    end
  end
end