Module: Mongoid::Association::Referenced::HasOne::Buildable
- Includes:
- WithPolymorphicCriteria
- Included in:
- Mongoid::Association::Referenced::HasOne
- Defined in:
- lib/mongoid/association/referenced/has_one/buildable.rb
Overview
The Builder behavior for has_one associations.
Instance Method Summary collapse
-
#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 after clearing the associated object.
Methods included from WithPolymorphicCriteria
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 after clearing the associated object.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mongoid/association/referenced/has_one/buildable.rb', line 23 def build(base, object, _type = nil, selected_fields = nil) if query?(object) # Handle array of hashes from $lookup aggregation if object.is_a?(Array) && object.all? { |o| o.is_a?(Hash) } doc = object.first return doc ? Factory.execute_from_db(klass, doc, nil, selected_fields, execute_callbacks: false) : nil end execute_query(object, base) unless base.new_record? else clear_associated(object) object end end |