module Mongoid::Association::Macros::ClassMethods
Class methods for associations.
Public Instance Methods
Adds a referenced association from the child Document
to a Document
in another database or collection.
@example Define the association.
class Game include Mongoid::Document belongs_to :person end class Person include Mongoid::Document has_one :game end
@param [ Symbol ] name The name of the association. @param [ Hash ] options The association options. @param &block Optional block for defining extensions.
# File lib/mongoid/association/macros.rb, line 148 def belongs_to(name, options = {}, &block) define_association!(__method__, name, options, &block) end
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.
@example Define the association.
class Person include Mongoid::Document embeds_many :addresses end class Address include Mongoid::Document embedded_in :person end
@param [ Symbol ] name The name of the association. @param [ Hash ] options The association options. @param &block Optional block for defining extensions.
# File lib/mongoid/association/macros.rb, line 80 def embedded_in(name, options = {}, &block) define_association!(__method__, name, options, &block) end
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.
@example Define the association.
class Person include Mongoid::Document embeds_many :addresses end class Address include Mongoid::Document embedded_in :person end
@param [ Symbol ] name The name of the association. @param [ Hash ] options The association options. @param &block Optional block for defining extensions.
# File lib/mongoid/association/macros.rb, line 103 def embeds_many(name, options = {}, &block) define_association!(__method__, name, options, &block) end
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.
@example Define the association.
class Person include Mongoid::Document embeds_one :name end class Name include Mongoid::Document embedded_in :person end
@param [ Symbol ] name The name of the association. @param [ Hash ] options The association options. @param &block Optional block for defining extensions.
# File lib/mongoid/association/macros.rb, line 126 def embeds_one(name, options = {}, &block) define_association!(__method__, name, options, &block) end
Adds a referenced many-to-many association between many of this Document
and many of another Document
.
@example Define the association.
class Person include Mongoid::Document has_and_belongs_to_many :preferences end class Preference include Mongoid::Document has_and_belongs_to_many :people end
@param [ Symbol ] name The name of the association. @param [ Hash ] options The association options. @param &block Optional block for defining extensions.
# File lib/mongoid/association/macros.rb, line 194 def has_and_belongs_to_many(name, options = {}, &block) define_association!(__method__, name, options, &block) end
Adds a referenced association from a parent Document
to many Documents in another database or collection.
@example Define the association.
class Person include Mongoid::Document has_many :posts end class Game include Mongoid::Document belongs_to :person end
@param [ Symbol ] name The name of the association. @param [ Hash ] options The association options. @param &block Optional block for defining extensions.
# File lib/mongoid/association/macros.rb, line 172 def has_many(name, options = {}, &block) define_association!(__method__, name, options, &block) end
Adds a referenced association from the child Document
to a Document
in another database or collection.
@example Define the association.
class Game include Mongoid::Document belongs_to :person end class Person include Mongoid::Document has_one :game end
@param [ Symbol ] name The name of the association. @param [ Hash ] options The association options. @param &block Optional block for defining extensions.
# File lib/mongoid/association/macros.rb, line 216 def has_one(name, options = {}, &block) define_association!(__method__, name, options, &block) end
Private Instance Methods
rubocop:enable Naming/PredicateName
# File lib/mongoid/association/macros.rb, line 224 def define_association!(macro_name, name, options = {}, &block) Association::MACRO_MAPPING[macro_name].new(self, name, options, &block).tap do |assoc| assoc.setup! self.relations = relations.merge(name => assoc) if assoc.embedded? && assoc.respond_to?(:store_as) && assoc.store_as != name aliased_associations[assoc.store_as] = name stored_as_associations << assoc.store_as end end end