Create the new builder for nested attributes on one-to-many relations.
@example Initialize the builder.
One.new(metadata, attributes, options)
@param [ Metadata ] metadata The relation metadata. @param [ Hash ] attributes The attributes hash to attempt to set. @param [ Hash ] options The options defined.
# File lib/mongoid/relations/builders/nested_attributes/many.rb, line 44 def initialize(metadata, 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 = metadata @options = options end
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 Build the nested attrs.
many.build(person)
@param [ Document ] parent The parent document of the relation. @param [ Hash ] options The mass assignment options.
@return [ Array ] The attributes.
# File lib/mongoid/relations/builders/nested_attributes/many.rb, line 21 def build(parent, options = {}) @existing = parent.send(metadata.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_attributes(parent, attrs.with_indifferent_access, options) else process_attributes(parent, attrs[1].with_indifferent_access, options) end end end
Generated with the Darkfish Rdoc Generator 2.