Class: Mongoid::Relations::Builders::NestedAttributes::Many

Inherits:
NestedBuilder show all
Defined in:
lib/mongoid/relations/builders/nested_attributes/many.rb

Instance Attribute Summary

Attributes inherited from NestedBuilder

#attributes, #existing, #metadata, #options

Instance Method Summary collapse

Methods inherited from NestedBuilder

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

Constructor Details

#initialize(metadata, attributes, options = {}) ⇒ Many

Create the new builder for nested attributes on one-to-many relations.

Example:

One.new(metadata, attributes, options)

Options:

metadata: The relation metadata attributes: The attributes hash to attempt to set. options: The options defined.

Returns:

A new builder.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mongoid/relations/builders/nested_attributes/many.rb', line 52

def initialize(, attributes, options = {})
  if attributes.respond_to?(:with_indifferent_access)
    @attributes = attributes.with_indifferent_access.sort do |a, b|
      a[0].to_i <=> b[0].to_i
    end
  else
    @attributes = attributes
  end
  @metadata = 
  @options = options
end

Instance Method Details

#build(parent) ⇒ Object

Builds the relation depending on the attributes and the options passed to the macro.

This attempts to perform 3 operations, either one of an update of the existing relation, a replacement of the relation with a new document, or a removal of the relation.

Example:

many.build(person)

Options:

parent: The parent document of the relation.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongoid/relations/builders/nested_attributes/many.rb', line 22

def build(parent)
  @existing = parent.send(.name)
  if over_limit?(attributes)
    raise Errors::TooManyNestedAttributeRecords.new(existing, options[:limit])
  end
  attributes.each do |attrs|
    if attrs.respond_to?(:with_indifferent_access)
      process(attrs)
    else
      process(attrs[1])
    end
  end
end