Module: Mongoid::Associations
- Included in:
- Document
- Defined in:
- lib/mongoid/associations/factory.rb,
lib/mongoid/associations.rb,
lib/mongoid/associations/decorator.rb,
lib/mongoid/associations/has_one_association.rb,
lib/mongoid/associations/has_many_association.rb,
lib/mongoid/associations/belongs_to_association.rb
Overview
:nodoc:
Defined Under Namespace
Modules: Decorator Classes: BelongsToAssociation, Factory, HasManyAssociation, HasOneAssociation
Instance Method Summary collapse
-
#belongs_to(association_name) ⇒ Object
Adds the association back to the parent document.
-
#has_many(association_name) ⇒ Object
Adds the association from a parent document to its children.
-
#has_one(association_name) ⇒ Object
Adds the association from a parent document to its child.
Instance Method Details
#belongs_to(association_name) ⇒ Object
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.
Options:
association_name: A Symbol
that matches the name of the parent class.
Example:
class Person < Mongoid::Document
has_many :addresses
end
class Address < Mongoid::Document
belongs_to :person
end
28 29 30 31 |
# File 'lib/mongoid/associations.rb', line 28 def belongs_to(association_name) @embedded = true add_association(:belongs_to, association_name.to_s.classify, association_name) end |
#has_many(association_name) ⇒ Object
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.
Options:
association_name: A Symbol
that is the plural child class name.
Example:
class Person < Mongoid::Document
has_many :addresses
end
class Address < Mongoid::Document
belongs_to :person
end
50 51 52 |
# File 'lib/mongoid/associations.rb', line 50 def has_many(association_name) add_association(:has_many, association_name.to_s.classify, association_name) end |
#has_one(association_name) ⇒ Object
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.
Options:
association_name: A Symbol
that is the plural child class name.
Example:
class Person < Mongoid::Document
has_many :addresses
end
class Address < Mongoid::Document
belongs_to :person
end
71 72 73 |
# File 'lib/mongoid/associations.rb', line 71 def has_one(association_name) add_association(:has_one, association_name.to_s.titleize, association_name) end |