Module: MongoMapper::Associations::ClassMethods
- Defined in:
- lib/mongo_mapper/associations.rb
Instance Method Summary collapse
- #associations ⇒ Object
-
#belongs_to(association_id, options = {}, &extension) ⇒ Object
This macro allows you define a “belongs-to” relationship between one document and some other document.
-
#many(association_id, options = {}, &extension) ⇒ Object
This macro allows you to define a “has-many” relationship between a document, and numerous child documents.
Instance Method Details
#associations ⇒ Object
122 123 124 125 126 |
# File 'lib/mongo_mapper/associations.rb', line 122 def associations @associations ||= self.superclass.respond_to?(:associations) ? self.superclass.associations : HashWithIndifferentAccess.new end |
#belongs_to(association_id, options = {}, &extension) ⇒ Object
This macro allows you define a “belongs-to” relationship between one document and some other document.
Requirements
Usage of this macro requires that your document define a key that can be used to store the ID of the target document that is the parent of this document.
Conventions
The following is a list of the conventions used by MongoMapper in defining a belongs-to relationship. Each can likely be overridden via the options
parameter.
-
The name of your belongs-to association is the lowercase, singular name of the target class
-
A key with the name of your association exists with an “_id” suffix to store the ID of the target of this relationship
84 85 86 87 |
# File 'lib/mongo_mapper/associations.rb', line 84 def belongs_to(association_id, ={}, &extension) create_association(:belongs_to, association_id, , &extension) self end |
#many(association_id, options = {}, &extension) ⇒ Object
This macro allows you to define a “has-many” relationship between a document, and numerous child documents.
Conventions
The following is a list of the conventions used by MongoMapper in defining this relationship. Each can likely be overridden via the options
parameter.
-
The name of your association is the lowercase, plural name of the target class
-
Your target class must have a “foreign key” bearing the name of this class suffixed by “_id”
117 118 119 120 |
# File 'lib/mongo_mapper/associations.rb', line 117 def many(association_id, ={}, &extension) create_association(:many, association_id, , &extension) self end |