class Mongoid::Association::Referenced::HasMany

The has_many 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

FOREIGN_KEY_SUFFIX

The default foreign key suffix.

@return [ String ] ‘_id’

@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

add_polymorphic_criterion(criteria, object_class) click to toggle source

Add polymorphic query criteria to a Criteria object, if this association is

polymorphic.

@param [ Mongoid::Criteria ] criteria The criteria object to add to. @param [ Class ] object_class The object class.

@return [ Mongoid::Criteria ] The criteria object.

@since 7.0

# File lib/mongoid/association/referenced/has_many.rb, line 163
def add_polymorphic_criterion(criteria, object_class)
  if polymorphic?
    criteria.where(type => object_class.name)
  else
    criteria
  end
end
bindable?(doc) click to toggle source

Whether trying to bind an object using this association should raise an error.

@param [ Document ] doc The document to be bound.

@return [ true, false ] Whether the document can be bound.

# File lib/mongoid/association/referenced/has_many.rb, line 186
def bindable?(doc)
  forced_nil_inverse? || (!!inverse && doc.fields.keys.include?(foreign_key))
end
criteria(base) click to toggle source

The criteria used for querying this association.

@return [ Mongoid::Criteria ] The criteria used for querying this association.

@since 7.0

# File lib/mongoid/association/referenced/has_many.rb, line 139
def criteria(base)
  query_criteria(base.send(primary_key), base)
end
embedded?() click to toggle source

Is this association type embedded?

@return [ false ] Always false.

@since 7.0

# File lib/mongoid/association/referenced/has_many.rb, line 109
def embedded?; false; end
foreign_key() click to toggle source

Get the foreign key field on the inverse for saving the association reference.

@return [ String ] The foreign key field on the inverse for saving the

association reference.

@since 7.0

# File lib/mongoid/association/referenced/has_many.rb, line 99
def foreign_key
  @foreign_key ||= @options[:foreign_key] ? @options[:foreign_key].to_s :
                     default_foreign_key_field
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/referenced/has_many.rb, line 198
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 [ Root ] The root atomic path calculator.

@since 2.1.0

# File lib/mongoid/association/referenced/has_many.rb, line 212
def path(document)
  Mongoid::Atomic::Paths::Root.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/referenced/has_many.rb, line 176
def polymorphic?
  @polymorphic ||= !!as
end
relation() click to toggle source

Get the association proxy class for this association type.

@return [ Association::HasMany::Proxy ] The proxy class.

@since 7.0

# File lib/mongoid/association/referenced/has_many.rb, line 130
def relation
  Proxy
end
relation_complements() click to toggle source

The list of association complements.

@return [ Array<Association> ] The association complements.

@since 7.0

# File lib/mongoid/association/referenced/has_many.rb, line 60
def relation_complements
  @relation_complements ||= [ Referenced::BelongsTo ].freeze
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/referenced/has_many.rb, line 69
def setup!
  setup_instance_methods!
  self
end
setup_instance_methods!() click to toggle source

Setup the instance methods on the class having this association type.

@return [ self ]

@since 7.0

# File lib/mongoid/association/referenced/has_many.rb, line 79
def setup_instance_methods!
  define_getter!
  define_ids_getter!
  define_setter!
  define_ids_setter!
  define_existence_check!
  define_autosaver!
  polymorph!
  define_dependency!
  @owner_class.validates_associated(name) if validate?
  self
end
stores_foreign_key?() click to toggle source

Does this association type store the foreign key?

@return [ true ] Always true.

@since 7.0

# File lib/mongoid/association/referenced/has_many.rb, line 123
def stores_foreign_key?; false; end
type() click to toggle source

The type of this association if it’s polymorphic.

@note Only relevant for polymorphic associations.

@return [ String, nil ] The type field.

@since 7.0

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

The default for validation the association object.

@return [ true ] Always true.

@since 7.0

# File lib/mongoid/association/referenced/has_many.rb, line 116
def validation_default; true; end

Private Instance Methods

default_foreign_key_field() click to toggle source
# File lib/mongoid/association/referenced/has_many.rb, line 218
def default_foreign_key_field
  @default_foreign_key_field ||= "#{inverse}#{FOREIGN_KEY_SUFFIX}"
end
default_primary_key() click to toggle source
# File lib/mongoid/association/referenced/has_many.rb, line 239
def default_primary_key
  PRIMARY_KEY_DEFAULT
end
determine_inverses(other) click to toggle source
# File lib/mongoid/association/referenced/has_many.rb, line 226
def determine_inverses(other)
  matches = (other || relation_class).relations.values.select do |rel|
    relation_complements.include?(rel.class) &&
        rel.relation_class_name == inverse_class_name

  end
  if matches.size > 1
    return [ default_inverse.name ] if default_inverse
    raise Errors::AmbiguousRelationship.new(relation_class, @owner_class, name, matches)
  end
  matches.collect { |m| m.name } unless matches.blank?
end
polymorphic_inverses(other) click to toggle source
# File lib/mongoid/association/referenced/has_many.rb, line 222
def polymorphic_inverses(other)
  [ as ]
end
query_criteria(object, base) click to toggle source
# File lib/mongoid/association/referenced/has_many.rb, line 243
def query_criteria(object, base)
  crit = klass.where(foreign_key => object)
  crit = with_polymorphic_criterion(crit, base)
  crit.association = self
  crit.parent_document = base
  with_ordering(crit)
end
with_inverse_field_criterion(criteria) click to toggle source
# File lib/mongoid/association/referenced/has_many.rb, line 267
def with_inverse_field_criterion(criteria)
  inverse_association = inverse_association(klass)
  if inverse_association.try(:inverse_of)
    criteria.any_in(inverse_association.inverse_of => [name, nil])
  else
    criteria
  end
end
with_ordering(criteria) click to toggle source
# File lib/mongoid/association/referenced/has_many.rb, line 259
def with_ordering(criteria)
  if order
    criteria.order_by(order)
  else
    criteria
  end
end
with_polymorphic_criterion(criteria, base) click to toggle source
# File lib/mongoid/association/referenced/has_many.rb, line 251
def with_polymorphic_criterion(criteria, base)
  if polymorphic?
    criteria.where(type => base.class.name)
  else
    criteria
  end
end