module Mongoid::Relations

All classes and modules under the relations namespace handle the functionality that has to do with embedded and referenced (relational) associations.

Attributes

__metadata[RW]
relation_metadata[RW]

Public Instance Methods

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/relations.rb, line 61
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 relation.

@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/relations.rb, line 73
def embedded_many?
  __metadata && __metadata.macro == :embeds_many
end
embedded_one?() click to toggle source

Determine if the document is part of an embeds_one relation.

@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/relations.rb, line 85
def embedded_one?
  __metadata && __metadata.macro == :embeds_one
end
metadata_name() click to toggle source

Get the metadata name for this document. If no metadata was defined will raise an error.

@example Get the metadata name.

document.metadata_name

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

@return [ Symbol ] The metadata name.

@since 3.0.0

# File lib/mongoid/relations.rb, line 100
def metadata_name
  raise Errors::NoMetadata.new(self.class.name) unless __metadata
  __metadata.name
end
referenced_many?() click to toggle source

Determine if the document is part of an references_many relation.

@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/relations.rb, line 113
def referenced_many?
  __metadata && __metadata.macro == :has_many
end
referenced_one?() click to toggle source

Determine if the document is part of an references_one relation.

@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/relations.rb, line 125
def referenced_one?
  __metadata && __metadata.macro == :has_one
end
reload_relations() click to toggle source

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

@example Reload the relations.

document.reload_relations

@return [ Hash ] The relations metadata.

@since 2.1.6

# File lib/mongoid/relations.rb, line 138
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