Module: MongoMapper::Associations::ClassMethods

Defined in:
lib/mongo_mapper/associations.rb

Instance Method Summary collapse

Instance Method Details

#associationsObject



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

Examples:

Conventional, and simple, usage of belongs_to

class Novel
  include MongoMapper::Document

  key :author_id, String # our "foreign key"

  belongs_to :author
end

Using :foreign_key and :class_name

class Pet
  include MongoMapper::Document

  key :person_id, String

  belongs_to :owner,
    :foreign_key => :person_id,
    :class_name  => "Person"
end

Defining a polymorphic belongs-to relationship

class Vehicle
  include MongoMapper::Document

  key :owner_id,   String
  key :owner_type, String

  belongs_to :owner,
    :polymorphic => true
end

Non-standard polymorphic belongs-to relationship

class Vehicle
  include MongoMapper::Document

  key :person_id,   String
  key :person_type, String

  belongs_to :owner,
    :polymorphic   => true,
    :foreign_key   => "person_id",
    :type_key_name => "person_type"
end

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):

  • :polymorphic (Boolean) — default: false

    Set this option to true to define a relationship that can be between this document and any other type of document. Note that you must also have a key on your document to store the type of document in this relationship.

  • :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



84
85
86
87
# File 'lib/mongo_mapper/associations.rb', line 84

def belongs_to(association_id, options={}, &extension)
  create_association(:belongs_to, association_id, options, &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”

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