module Mongoid::Association::Referenced::BelongsTo::Buildable
The Builder behavior for belongs_to associations.
@since 7.0
Public Instance Methods
build(base, object, type = nil, selected_fields = nil)
click to toggle source
This method either takes an _id or an object and queries for the inverse side using the id or sets the object.
@example Build the document.
relation.build(meta, attrs)
@param [ Object
] base The base object. @param [ Object
] object The object to use to build the association. @param [ String ] type The type of the association. @param [ nil ] selected_fields Must be nil.
@return [ Document
] A single document.
# File lib/mongoid/association/referenced/belongs_to/buildable.rb, line 26 def build(base, object, type = nil, selected_fields = nil) return object unless query?(object) execute_query(object, type) end
Private Instance Methods
execute_query(object, type)
click to toggle source
# File lib/mongoid/association/referenced/belongs_to/buildable.rb, line 33 def execute_query(object, type) query_criteria(object, type).limit(1).first(id_sort: :none) end
query?(object)
click to toggle source
# 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)
click to toggle source
# File lib/mongoid/association/referenced/belongs_to/buildable.rb, line 37 def query_criteria(object, type) model = type ? type.constantize : relation_class model.where(primary_key => object) end