Method: MongoMapper::Associations::ClassMethods#many

Defined in:
lib/mongo_mapper/associations.rb

#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”

Parameters:

  • association_id (Symbol)

    The name of this association

  • options (Hash) (defaults to: {})

    Optional parameters that define the characteristics of this relationship. These are often used to override MongoMapper conventions.

Options Hash (options):

  • :class_name (String)

    If your relationship doesn’t use the name of some class, you must use this option to indicate the target class for this relationship.

  • :foreign_key (Symbol)

    Use this option to specify a non-conventional key that stores the ID of the parent in this relationship

  • :as (#to_s)

    Used when the target relationship is polymorphic (i.e. the belongs_to has set :polymorphic to true). See examples for usage.



117
118
119
120
# File 'lib/mongo_mapper/associations.rb', line 117

def many(association_id, options={}, &extension)
  create_association(:many, association_id, options, &extension)
  self
end