Class: Mongoid::Relations::Builders::Embedded::Many

Inherits:
Mongoid::Relations::Builder show all
Defined in:
lib/mongoid/relations/builders/embedded/many.rb

Overview

Since:

Instance Attribute Summary

Attributes inherited from Mongoid::Relations::Builder

#base, #metadata, #object

Instance Method Summary (collapse)

Methods inherited from Mongoid::Relations::Builder

#initialize

Constructor Details

This class inherits a constructor from Mongoid::Relations::Builder

Instance Method Details

- (Array<Document ] The documents.) build(type = nil)

Builds the document out of the attributes using the provided metadata on the relation. Instantiates through the factory in order to make sure subclasses and allocation are used if fitting. This case will return many documents.

Examples:

Build the documents.

Builder.new(meta, attrs).build

Since:

  • 2.0.0.rc.1



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/relations/builders/embedded/many.rb', line 19

def build(type = nil)
  return [] if object.blank?
  return object if object.first.is_a?(Document)
  docs = []
  object.each do |attrs|
    if _loading? && base.persisted?
      docs.push(Factory.from_db(klass, attrs))
    else
      docs.push(Factory.build(klass, attrs))
    end
  end
  docs
end