Module: Mongoid::Relations::Macros::ClassMethods
- Defined in:
- lib/mongoid/relations/macros.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#belongs_to(name, options = {}, &block) ⇒ Object
(also: #belongs_to_related, #referenced_in)
Adds a relational association from the child Document to a Document in another database or collection.
-
#embedded_in(name, options = {}, &block) ⇒ Object
Adds the relation back to the parent document.
-
#embeds_many(name, options = {}, &block) ⇒ Object
Adds the relation from a parent document to its children.
-
#embeds_one(name, options = {}, &block) ⇒ Object
Adds the relation from a parent document to its child.
-
#has_and_belongs_to_many(name, options = {}, &block) ⇒ Object
(also: #references_and_referenced_in_many)
Adds a relational many-to-many association between many of this Document and many of another Document.
-
#has_many(name, options = {}, &block) ⇒ Object
(also: #has_many_related, #references_many)
Adds a relational association from a parent Document to many Documents in another database or collection.
-
#has_one(name, options = {}, &block) ⇒ Object
(also: #has_one_related, #references_one)
Adds a relational association from the child Document to a Document in another database or collection.
Instance Method Details
#belongs_to(name, options = {}, &block) ⇒ Object Also known as: referenced_in ,
Adds a relational association from the child Document to a Document in another database or collection.
129 130 131 132 133 134 135 136 |
# File 'lib/mongoid/relations/macros.rb', line 129 def belongs_to(name, = {}, &block) characterize(name, Referenced::In, , &block).tap do || relate(name, ) reference() autosave() validates_relation() end end |
#embedded_in(name, options = {}, &block) ⇒ Object
Adds the relation 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 relation calling persistence methods on the child object will cause a save to fail.
51 52 53 54 55 56 |
# File 'lib/mongoid/relations/macros.rb', line 51 def (name, = {}, &block) characterize(name, Embedded::In, , &block).tap do || self. = true relate(name, ) end end |
#embeds_many(name, options = {}, &block) ⇒ Object
Adds the relation from a parent document to its children. The name of the relation needs to be a pluralized form of the child class name.
77 78 79 80 81 82 |
# File 'lib/mongoid/relations/macros.rb', line 77 def (name, = {}, &block) characterize(name, Embedded::Many, , &block).tap do || relate(name, ) validates_relation() end end |
#embeds_one(name, options = {}, &block) ⇒ Object
Adds the relation from a parent document to its child. The name of the relation needs to be a singular form of the child class name.
103 104 105 106 107 108 109 |
# File 'lib/mongoid/relations/macros.rb', line 103 def (name, = {}, &block) characterize(name, Embedded::One, , &block).tap do || relate(name, ) builder(name, ).creator(name) validates_relation() end end |
#has_and_belongs_to_many(name, options = {}, &block) ⇒ Object Also known as: references_and_referenced_in_many
Adds a relational many-to-many association between many of this Document and many of another Document.
189 190 191 192 193 194 195 196 197 |
# File 'lib/mongoid/relations/macros.rb', line 189 def has_and_belongs_to_many(name, = {}, &block) characterize(name, Referenced::ManyToMany, , &block).tap do || relate(name, ) reference(, Array) autosave() validates_relation() synced() end end |
#has_many(name, options = {}, &block) ⇒ Object Also known as: references_many ,
Adds a relational association from a parent Document to many Documents in another database or collection.
158 159 160 161 162 163 164 165 |
# File 'lib/mongoid/relations/macros.rb', line 158 def has_many(name, = {}, &block) characterize(name, Referenced::Many, , &block).tap do || relate(name, ) reference() autosave() validates_relation() end end |
#has_one(name, options = {}, &block) ⇒ Object Also known as: references_one ,
Adds a relational association from the child Document to a Document in another database or collection.
218 219 220 221 222 223 224 225 |
# File 'lib/mongoid/relations/macros.rb', line 218 def has_one(name, = {}, &block) characterize(name, Referenced::One, , &block).tap do || relate(name, ) reference() builder(name, ).creator(name).autosave() validates_relation() end end |