Module: Mongoid::Relations::Macros::ClassMethods
- Defined in:
- lib/mongoid/relations/macros.rb
Instance Method Summary collapse
-
#belongs_to(name, options = {}, &block) ⇒ Object
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
Adds a relational many-to-many association between many of this Document and many of another Document.
-
#has_many(name, options = {}, &block) ⇒ Object
Adds a relational association from a parent Document to many Documents in another database or collection.
-
#has_one(name, options = {}, &block) ⇒ Object
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
Adds a relational association from the child Document to a Document in another database or collection.
137 138 139 140 141 142 143 |
# File 'lib/mongoid/relations/macros.rb', line 137 def belongs_to(name, = {}, &block) = reference_one_to_one(name, , Referenced::In, &block) aliased_fields[name.to_s] = .foreign_key touchable() add_counter_cache_callbacks() if .counter_cached? 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.
53 54 55 56 57 58 59 60 |
# File 'lib/mongoid/relations/macros.rb', line 53 def (name, = {}, &block) = characterize(name, Embedded::In, , &block) self. = true relate(name, ) builder(name, ).creator(name, ) add_counter_cache_callbacks() if .counter_cached? 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.
81 82 83 84 85 86 87 88 |
# File 'lib/mongoid/relations/macros.rb', line 81 def (name, = {}, &block) = characterize(name, Embedded::Many, , &block) self.cyclic = true if [:cyclic] relate(name, ) (name, ) validates_relation() 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.
109 110 111 112 113 114 115 116 117 |
# File 'lib/mongoid/relations/macros.rb', line 109 def (name, = {}, &block) = characterize(name, Embedded::One, , &block) self.cyclic = true if [:cyclic] relate(name, ) (name, ) builder(name, ).creator(name, ) validates_relation() end |
#has_and_belongs_to_many(name, options = {}, &block) ⇒ Object
Adds a relational many-to-many association between many of this Document and many of another Document.
193 194 195 196 197 198 199 200 201 |
# File 'lib/mongoid/relations/macros.rb', line 193 def has_and_belongs_to_many(name, = {}, &block) = characterize(name, Referenced::ManyToMany, , &block) relate(name, ) reference(, Array) autosave() validates_relation() synced() end |
#has_many(name, options = {}, &block) ⇒ Object
Adds a relational association from a parent Document to many Documents in another database or collection.
163 164 165 166 167 168 169 170 171 |
# File 'lib/mongoid/relations/macros.rb', line 163 def has_many(name, = {}, &block) = characterize(name, Referenced::Many, , &block) relate(name, ) ids_getter(name, ).ids_setter(name, ) reference() autosave() validates_relation() end |
#has_one(name, options = {}, &block) ⇒ Object
Adds a relational association from the child Document to a Document in another database or collection.
221 222 223 |
# File 'lib/mongoid/relations/macros.rb', line 221 def has_one(name, = {}, &block) reference_one_to_one(name, , Referenced::One, &block) end |