Module: Mongoid::Relations

Extended by:
ActiveSupport::Concern
Includes:
Accessors, AutoSave, Builders, Cascading, CounterCache, Cyclic, Macros, Polymorphic, Reflections, Synchronization, Touchable
Included in:
Composable
Defined in:
lib/mongoid/relations.rb,
lib/mongoid/relations/one.rb,
lib/mongoid/relations/many.rb,
lib/mongoid/relations/proxy.rb,
lib/mongoid/relations/eager.rb,
lib/mongoid/relations/macros.rb,
lib/mongoid/relations/cyclic.rb,
lib/mongoid/relations/builder.rb,
lib/mongoid/relations/options.rb,
lib/mongoid/relations/binding.rb,
lib/mongoid/relations/metadata.rb,
lib/mongoid/relations/builders.rb,
lib/mongoid/relations/cascading.rb,
lib/mongoid/relations/accessors.rb,
lib/mongoid/relations/auto_save.rb,
lib/mongoid/relations/touchable.rb,
lib/mongoid/relations/eager/base.rb,
lib/mongoid/relations/constraint.rb,
lib/mongoid/relations/conversions.rb,
lib/mongoid/relations/reflections.rb,
lib/mongoid/relations/polymorphic.rb,
lib/mongoid/relations/embedded/in.rb,
lib/mongoid/relations/marshalable.rb,
lib/mongoid/relations/embedded/one.rb,
lib/mongoid/relations/eager/has_one.rb,
lib/mongoid/relations/embedded/many.rb,
lib/mongoid/relations/referenced/in.rb,
lib/mongoid/relations/counter_cache.rb,
lib/mongoid/relations/nested_builder.rb,
lib/mongoid/relations/referenced/one.rb,
lib/mongoid/relations/eager/has_many.rb,
lib/mongoid/relations/referenced/many.rb,
lib/mongoid/relations/synchronization.rb,
lib/mongoid/relations/eager/belongs_to.rb,
lib/mongoid/relations/cascading/delete.rb,
lib/mongoid/relations/cascading/destroy.rb,
lib/mongoid/relations/cascading/nullify.rb,
lib/mongoid/relations/embedded/batchable.rb,
lib/mongoid/relations/cascading/restrict.rb,
lib/mongoid/relations/targets/enumerable.rb,
lib/mongoid/relations/bindings/embedded/in.rb,
lib/mongoid/relations/builders/embedded/in.rb,
lib/mongoid/relations/builders/embedded/one.rb,
lib/mongoid/relations/bindings/embedded/one.rb,
lib/mongoid/relations/bindings/referenced/in.rb,
lib/mongoid/relations/builders/embedded/many.rb,
lib/mongoid/relations/bindings/embedded/many.rb,
lib/mongoid/relations/builders/referenced/in.rb,
lib/mongoid/relations/bindings/referenced/one.rb,
lib/mongoid/relations/builders/referenced/one.rb,
lib/mongoid/relations/referenced/many_to_many.rb,
lib/mongoid/relations/builders/referenced/many.rb,
lib/mongoid/relations/bindings/referenced/many.rb,
lib/mongoid/relations/eager/has_and_belongs_to_many.rb,
lib/mongoid/relations/builders/nested_attributes/one.rb,
lib/mongoid/relations/builders/nested_attributes/many.rb,
lib/mongoid/relations/bindings/referenced/many_to_many.rb,
lib/mongoid/relations/builders/referenced/many_to_many.rb

Overview

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

Defined Under Namespace

Modules: Accessors, AutoSave, Bindings, Builders, Cascading, Conversions, CounterCache, Cyclic, Eager, Embedded, Macros, Marshalable, Options, Polymorphic, Referenced, Reflections, Synchronization, Targets, Touchable Classes: Binding, Builder, Constraint, Many, Metadata, NestedBuilder, One, Proxy

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from CounterCache

#reset_counters

Methods included from Touchable

#touch

Methods included from Synchronization

#remove_inverse_keys, #syncable?, #synced, #synced?, #update_inverse_keys

Methods included from Reflections

#reflect_on_all_associations, #reflect_on_association

Methods included from Macros

#associations

Methods included from Cascading

#cascade!

Methods included from AutoSave

#__autosaving__, #autosaved?, #changed_for_autosave?

Methods included from Accessors

#__build__, #create_relation, #reset_relation_criteria, #set_relation

Instance Attribute Details

- (Object) __metadata Also known as: relation_metadata

Returns the value of attribute __metadata



49
50
51
# File 'lib/mongoid/relations.rb', line 49

def 
  @__metadata
end

Instance Method Details

- (true, false) embedded?

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

Examples:

Is the document embedded?

address.embedded?

Since:

  • 2.0.0.rc.1



61
62
63
# File 'lib/mongoid/relations.rb', line 61

def embedded?
  @embedded ||= (cyclic ? _parent.present? : self.class.embedded?)
end

- (true, false) embedded_many?

Determine if the document is part of an embeds_many relation.

Examples:

Is the document in an embeds many?

address.embedded_many?

Since:

  • 2.0.0.rc.1



73
74
75
# File 'lib/mongoid/relations.rb', line 73

def embedded_many?
   && .macro == :embeds_many
end

- (true, false) embedded_one?

Determine if the document is part of an embeds_one relation.

Examples:

Is the document in an embeds one?

address.embedded_one?

Since:

  • 2.0.0.rc.1



85
86
87
# File 'lib/mongoid/relations.rb', line 85

def embedded_one?
   && .macro == :embeds_one
end

- (Symbol) metadata_name

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

Examples:

Get the metadata name.

document.

Raises:

Since:

  • 3.0.0



100
101
102
103
# File 'lib/mongoid/relations.rb', line 100

def 
  raise Errors::NoMetadata.new(self.class.name) unless 
  .name
end

- (true, false) referenced_many?

Determine if the document is part of an references_many relation.

Examples:

Is the document in a references many?

post.referenced_many?

Since:

  • 2.0.0.rc.1



113
114
115
# File 'lib/mongoid/relations.rb', line 113

def referenced_many?
   && .macro == :has_many
end

- (true, false) referenced_one?

Determine if the document is part of an references_one relation.

Examples:

Is the document in a references one?

address.referenced_one?

Since:

  • 2.0.0.rc.1



125
126
127
# File 'lib/mongoid/relations.rb', line 125

def referenced_one?
   && .macro == :has_one
end

- (Hash) reload_relations

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

Examples:

Reload the relations.

document.reload_relations

Since:

  • 2.1.6



138
139
140
141
142
143
144
145
146
# 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