lib/mongoid/association/accessors.rb
# Create an association from an object and association metadata.
#
# @example Create the association.
#   person.create_relation(document, association)
#
# @param [ Document, Array<Document> ] object The association target.
# @param [ Association ] association The association metadata.
# @param [ Hash ] selected_fields Fields which were retrieved via #only.
#   If selected_fields is specified, fields not listed in it will not be
#   accessible in the created association document.
#
# @return [ Proxy ] The association.
Create an association from an object and association metadata.
Create an association from an object and association metadata .
def create_relation(object, association, selected_fields = nil)
        type = @attributes[association.inverse_type]
        target = if t = association.build(self, object, type, selected_fields)
          association.create_relation(self, t)
        else
          nil
        end
        if association.embedded?
          Array(target).each do |doc|
            doc.try(:run_pending_callbacks)
          end
        end
        target
      end
def create_relation ( object , association , selected_fields = nil ) type = @attributes [ association . inverse_type ] target = if t = association . build ( self , object , type , selected_fields ) association . create_relation ( self , t ) else nil end if association . embedded? Array ( target ) . each do | doc | doc . try ( :run_pending_callbacks ) end end target end
Create an association from an object and association metadata.
Create an association from an object and association metadata .
# Only need to do this on embedded associations. The pending callbacks # are only added when materializing the documents, which only happens # on embedded associations. There is no call to the database in the # construction of a referenced association.
None
 Proxy 
object
<not_specific>
association
<not_specific>
selected_fields
<not_specific>
<not_specific>
selected_fields Fields which were retrieved via #only. If selected_fields is specified, fields not listed in it will not be accessible in the created association document.
selected_fields Fields which were retrieved via #only . If selected_fields is specified fields not listed in it will not be accessible in the created association document .
example
Create the association. person.create_relation(document, association)
Create the association . person . create_relation ( document association )