123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Association::Referenced::BelongsTo::Buildable

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

Overview

The Builder behavior for belongs_to associations.

Instance Method Summary

Instance Method Details

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

This method either takes an _id or an object and queries for the inverse side using the id or sets the object.

Examples:

Build the document.

relation.build(meta, attrs)

Parameters:

  • base (Object)

    The base object.

  • object (Object)

    The object to use to build the association.

  • type (String) (defaults to: nil)

    The type of the association.

  • selected_fields (nil) (defaults to: nil)

    Must be nil.

Returns:

[ GitHub ]

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

def build(base, object, type = nil, selected_fields = nil)
  return object unless query?(object)
  execute_query(object, type)
end

#execute_query(object, type) (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/belongs_to/buildable.rb', line 31

def execute_query(object, type)
  query_criteria(object, type).take
end

#query?(object) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/belongs_to/buildable.rb', line 42

def query?(object)
  object && !object.is_a?(Mongoid::Document)
end

#query_criteria(object, type) (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/belongs_to/buildable.rb', line 35

def query_criteria(object, type)
  cls = type ? (type.is_a?(String) ? type.constantize : type) : relation_class
  crit = cls.criteria
  crit = crit.apply_scope(scope)
  crit.where(primary_key => object)
end