module Mongoid::Association

Constants

MACRO_MAPPING

Map the macros to their corresponding Association classes.

@return [ Hash ] The mapping from macros to their Association class.

@since 7.0

Attributes

_association[RW]

Public Instance Methods

association_name() click to toggle source

Get the association name for this document. If no association was defined

an error will be raised.

@example Get the association name.

document.association_name

@raise [ Errors::NoMetadata ] If no association metadata is present.

@return [ Symbol ] The association name.

@since 3.0.0

# File lib/mongoid/association.rb, line 104
def association_name
  raise Errors::NoMetadata.new(self.class.name) unless _association
  _association.name
end
embedded?() click to toggle source

Determine if the document itself is embedded in another document via the proper channels. (If it has a parent document.)

@example Is the document embedded?

address.embedded?

@return [ true, false ] True if the document has a parent document.

@since 2.0.0.rc.1

# File lib/mongoid/association.rb, line 65
def embedded?
  @embedded ||= (cyclic ? _parent.present? : self.class.embedded?)
end
embedded_many?() click to toggle source

Determine if the document is part of an embeds_many association.

@example Is the document in an embeds many?

address.embedded_many?

@return [ true, false ] True if in an embeds many.

@since 2.0.0.rc.1

# File lib/mongoid/association.rb, line 77
def embedded_many?
  _association && _association.is_a?(Association::Embedded::EmbedsMany)
end
embedded_one?() click to toggle source

Determine if the document is part of an embeds_one association.

@example Is the document in an embeds one?

address.embedded_one?

@return [ true, false ] True if in an embeds one.

@since 2.0.0.rc.1

# File lib/mongoid/association.rb, line 89
def embedded_one?
  _association && _association.is_a?(Association::Embedded::EmbedsOne)
end
referenced_many?() click to toggle source

Determine if the document is part of an references_many association.

@example Is the document in a references many?

post.referenced_many?

@return [ true, false ] True if in a references many.

@since 2.0.0.rc.1

# File lib/mongoid/association.rb, line 117
def referenced_many?
  _association && _association.is_a?(Association::Referenced::HasMany)
end
referenced_one?() click to toggle source

Determine if the document is part of an references_one association.

@example Is the document in a references one?

address.referenced_one?

@return [ true, false ] True if in a references one.

@since 2.0.0.rc.1

# File lib/mongoid/association.rb, line 129
def referenced_one?
  _association && _association.is_a?(Association::Referenced::HasOne)
end
reload_relations() click to toggle source

Convenience method for iterating through the loaded associations and reloading them.

@example Reload the associations.

document.reload_relations

@return [ Hash ] The association metadata.

@since 2.1.6

# File lib/mongoid/association.rb, line 142
def reload_relations
  relations.each_pair do |name, meta|
    if instance_variable_defined?("@_#{name}")
      if _parent.nil? || instance_variable_get("@_#{name}") != _parent
        remove_instance_variable("@_#{name}")
      end
    end
  end
end