module Mongoid::Association::Referenced::HasAndBelongsToMany::Buildable

The Builder behavior for has_and_belongs_to_many associations.

@since 7.0

Public Instance Methods

build(base, object, type = nil, selected_fields = nil) click to toggle source

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

@example Build the documents.

relation.build(association, attrs)

@param [ Object ] base The base object. @param [ Object ] object The object to use to build the association. @param [ String ] type Not used in this context. @param [ nil ] selected_fields Must be nil.

@return [ Array<Document> ] The documents.

# File lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb, line 26
def build(base, object, type = nil, selected_fields = nil)
  if query?(object)
    query_criteria(object)
  else
    object.try(:dup)
  end
end

Private Instance Methods

query?(object) click to toggle source
# File lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb, line 36
def query?(object)
  object.nil? || Array(object).all? { |d| !d.is_a?(Mongoid::Document) }
end