Module: Mongoid::Association::Macros::ClassMethods
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Extended In:
| |
Defined in: | lib/mongoid/association/macros.rb |
Overview
Class methods for associations.
Instance Method Summary
-
#belongs_to(name, options = {}, &block)
Adds a referenced association from the child
::Mongoid::Document
to a::Mongoid::Document
in another database or collection. -
#embedded_in(name, options = {}, &block)
Adds the association back to the parent document.
-
#embeds_many(name, options = {}, &block)
Adds the association from a parent document to its children.
-
#embeds_one(name, options = {}, &block)
Adds the association from a parent document to its child.
-
#has_and_belongs_to_many(name, options = {}, &block)
Adds a referenced many-to-many association between many of this
::Mongoid::Document
and many of another::Mongoid::Document
. -
#has_many(name, options = {}, &block)
Adds a referenced association from a parent
::Mongoid::Document
to many Documents in another database or collection. -
#has_one(name, options = {}, &block)
Adds a referenced association from the child
::Mongoid::Document
to a::Mongoid::Document
in another database or collection. - #define_association!(macro_name, name, options = {}, &block) private
Instance Method Details
#belongs_to(name, options = {}, &block)
Adds a referenced association from the child ::Mongoid::Document
to a ::Mongoid::Document
in another database or collection.
# File 'lib/mongoid/association/macros.rb', line 148
def belongs_to(name, = {}, &block) define_association!(__method__, name, , &block) end
#define_association!(macro_name, name, options = {}, &block) (private)
[ GitHub ]# File 'lib/mongoid/association/macros.rb', line 224
def define_association!(macro_name, name, = {}, &block) Association::MACRO_MAPPING[macro_name].new(self, name, , &block).tap do |assoc| assoc.setup! self.relations = relations.merge(name => assoc) if assoc. && assoc.respond_to?(:store_as) && assoc.store_as != name aliased_associations[assoc.store_as] = name stored_as_associations << assoc.store_as end end end
#embedded_in(name, options = {}, &block)
Adds the association back to the parent document. This macro is necessary to set the references from the child back to the parent document. If a child does not define this association calling persistence methods on the child object will cause a save to fail.
# File 'lib/mongoid/association/macros.rb', line 80
def (name, = {}, &block) define_association!(__method__, name, , &block) end
#embeds_many(name, options = {}, &block)
Adds the association from a parent document to its children. The name of the association needs to be a pluralized form of the child class name.
# File 'lib/mongoid/association/macros.rb', line 103
def (name, = {}, &block) define_association!(__method__, name, , &block) end
#embeds_one(name, options = {}, &block)
Adds the association from a parent document to its child. The name of the association needs to be a singular form of the child class name.
# File 'lib/mongoid/association/macros.rb', line 126
def (name, = {}, &block) define_association!(__method__, name, , &block) end
#has_and_belongs_to_many(name, options = {}, &block)
Adds a referenced many-to-many association between many of this ::Mongoid::Document
and many of another ::Mongoid::Document
.
# File 'lib/mongoid/association/macros.rb', line 194
def has_and_belongs_to_many(name, = {}, &block) define_association!(__method__, name, , &block) end
#has_many(name, options = {}, &block)
Adds a referenced association from a parent ::Mongoid::Document
to many Documents in another database or collection.
# File 'lib/mongoid/association/macros.rb', line 172
def has_many(name, = {}, &block) define_association!(__method__, name, , &block) end
#has_one(name, options = {}, &block)
Adds a referenced association from the child ::Mongoid::Document
to a ::Mongoid::Document
in another database or collection.
# File 'lib/mongoid/association/macros.rb', line 216
def has_one(name, = {}, &block) define_association!(__method__, name, , &block) end