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
-
#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.
- #execute_query(object, type) private
- #query?(object) ⇒ Boolean private
- #query_criteria(object, type) private
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.
# 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) # Handle array from $lookup aggregation (returns array even for belongs_to) if object.is_a?(Array) first = object.first case first when nil, Mongoid::Document then return first when Hash then return Factory.execute_from_db(klass, first, nil, selected_fields, execute_callbacks: false) else raise ArgumentError, "Cannot build belongs_to association from array" end end # Handle single hash from $lookup with $unwind if object.is_a?(Hash) return Factory.execute_from_db(klass, object, nil, selected_fields, execute_callbacks: false) end execute_query(object, type) end
#execute_query(object, type) (private)
[ GitHub ]# File 'lib/mongoid/association/referenced/belongs_to/buildable.rb', line 47
def execute_query(object, type) query_criteria(object, type).take end