123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Association::Referenced::HasAndBelongsToMany::Buildable

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb

Overview

The Builder behavior for has_and_belongs_to_many associations.

Instance Method Summary

Instance Method Details

#build(base, object, type = nil, selected_fields = nil) ⇒ Array<Document>

This builder either takes a hash and queries for the object or an array of documents, where it will just return them.

Examples:

Build the documents.

relation.build(association, attrs)

Parameters:

  • base (Object)

    The base object.

  • object (Object)

    The object to use to build the association.

  • type (String) (defaults to: nil)

    Not used in this context.

  • selected_fields (nil) (defaults to: nil)

    Must be nil.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb', line 24

def build(base, object, type = nil, selected_fields = nil)
  if query?(object)
    query_criteria(object)
  else
    object.try(:dup)
  end
end

#query?(object) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb', line 34

def query?(object)
  object.nil? || Array(object).all? { |d| !d.is_a?(Mongoid::Document) }
end