class Mongoid::Association::Embedded::EmbedsMany

The EmbedsMany type association.

@since 7.0

Constants

ASSOCIATION_OPTIONS

The options available for this type of association, in addition to the common ones.

@return [ Array<Symbol> ] The extra valid options.

@since 7.0

VALID_OPTIONS

The complete list of valid options for this association, including the shared ones.

@return [ Array<Symbol> ] The valid options.

@since 7.0

Public Instance Methods

criteria(base, target) click to toggle source

Get a criteria object for searching given a parent and children documents.

@param [ Document ] base The base document. @param [ Document ] target The children documents.

@since 7.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 167
def criteria(base, target)
  criterion = klass.scoped
  criterion.embedded = true
  criterion.documents = target
  criterion.parent_document = base
  criterion.association = self
  apply_ordering(criterion)
end
embedded?() click to toggle source

Is this association type embedded?

@return [ true ] Always true.

@since 7.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 80
def embedded?; true; end
key() click to toggle source

The key that is used to get the attributes for the associated object.

@return [ String ] The name of the field used to store the association.

@since 7.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 71
def key
  store_as.to_s
end
nested_builder(attributes, options) click to toggle source

The nested builder object.

@param [ Hash ] attributes The attributes to use to build the association object. @param [ Hash ] options The options for the association.

@return [ Association::Nested::Many ] The Nested Builder object.

@since 7.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 142
def nested_builder(attributes, options)
  Nested::Many.new(self, attributes, options)
end
path(document) click to toggle source

Get the path calculator for the supplied document.

@example Get the path calculator.

Proxy.path(document)

@param [ Document ] document The document to calculate on.

@return [ Mongoid::Atomic::Paths::Embedded::Many ]

The embedded many atomic path calculator.

@since 2.1.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 157
def path(document)
  Mongoid::Atomic::Paths::Embedded::Many.new(document)
end
polymorphic?() click to toggle source

Is this association polymorphic?

@return [ true, false ] Whether this association is polymorphic.

@since 7.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 119
def polymorphic?
  @polymorphic ||= !!@options[:as]
end
primary_key() click to toggle source

The primary key

@return [ nil ] Not relevant for this association

# File lib/mongoid/association/embedded/embeds_many.rb, line 103
def primary_key; end
relation() click to toggle source

Get the association proxy class for this association type.

@return [ Association::Embedded::EmbedsMany::Proxy ] The proxy class.

@since 7.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 110
def relation
  Proxy
end
setup!() click to toggle source

Setup the instance methods, fields, etc. on the association owning class.

@return [ self ]

@since 7.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 50
def setup!
  setup_instance_methods!
  @owner_class.embedded_relations = @owner_class.embedded_relations.merge(name => self)
  @owner_class.aliased_fields[name.to_s] = store_as if store_as
  self
end
store_as() click to toggle source

The field key used to store the list of association objects.

@return [ String ] The field name.

@since 7.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 62
def store_as
  @store_as ||= (@options[:store_as].try(:to_s) || name.to_s)
end
stores_foreign_key?() click to toggle source

Does this association type store the foreign key?

@return [ false ] Always false.

@since 7.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 98
def stores_foreign_key?; false; end
type() click to toggle source

The field used to store the type of the related object.

@note Only relevant if the association is polymorphic.

@return [ String, nil ] The field for storing the associated object’s type.

@since 7.0

# File lib/mongoid/association/embedded/embeds_many.rb, line 130
def type
  @type ||= "#{as}_type" if polymorphic?
end
validation_default() click to toggle source

Get the default validation setting for the association. Determines if by default a validates associated will occur.

@example Get the validation default.

Proxy.validation_default

@return [ true ] Always true.

@since 2.1.9

# File lib/mongoid/association/embedded/embeds_many.rb, line 91
def validation_default; true; end

Private Instance Methods

apply_ordering(criteria) click to toggle source
# File lib/mongoid/association/embedded/embeds_many.rb, line 178
def apply_ordering(criteria)
  order ? criteria.order_by(order) : criteria
end
determine_inverses(other) click to toggle source
# File lib/mongoid/association/embedded/embeds_many.rb, line 200
def determine_inverses(other)
  matches = relation_class.relations.values.select do |rel|
    relation_complements.include?(rel.class) &&
      # https://jira.mongodb.org/browse/MONGOID-4882
      rel.relation_class_name.sub(/\A::/, '') == inverse_class_name
  end
  if matches.size > 1
    raise Errors::AmbiguousRelationship.new(relation_class, @owner_class, name, matches)
  end
  matches.collect { |m| m.name } unless matches.blank?
end
polymorphic_inverses(other = nil) click to toggle source
# File lib/mongoid/association/embedded/embeds_many.rb, line 196
def polymorphic_inverses(other = nil)
  [ as ]
end
relation_complements() click to toggle source
# File lib/mongoid/association/embedded/embeds_many.rb, line 192
def relation_complements
  @relation_complements ||= [ Embedded::EmbeddedIn ].freeze
end
setup_instance_methods!() click to toggle source
# File lib/mongoid/association/embedded/embeds_many.rb, line 182
def setup_instance_methods!
  define_getter!
  define_setter!
  define_existence_check!
  define_builder!
  define_creator!
  @owner_class.cyclic = true if cyclic?
  @owner_class.validates_associated(name) if validate?
end