Module: DataMapper::Model::Relationship

Module | DataMapper::Model::Relationship
In: | lib/dm-core/model/relationship.rb

### Methods

belongs_to extended has inherited n relationships

### Included Modules

Extlib::Assertions

### Public Class methods

extended(model)

Initializes relationships hash for extended model class.

When model calls has n, has 1 or belongs_to, relationships are stored in that hash: keys are repository names and values are relationship sets.

@api private

### Public Instance methods

belongs_to(name, *args)

A shorthand, clear syntax for defining many-to-one resource relationships.

     * belongs_to :user                              # many to one user
     * belongs_to :friend, :model => 'User'          # many to one friend
     * belongs_to :reference, :repository => :pubmed # association for repository other than default

@param name [Symbol]

      the name that the association will be referenced by

@param model [Model, to_str]

      the target model of the relationship

@param opts [Hash]

      an options hash

@option :model[Model, String] The name of the class to associate with, if omitted

      then the association name is assumed to match the class name

@option :repository[Symbol]

      name of child model repository

@return [Association::Relationship] The association created

      should not be accessed directly

@api public

has(cardinality, name, *args)

A shorthand, clear syntax for defining one-to-one, one-to-many and many-to-many resource relationships.

     * has 1,    :friend                             # one friend
     * has n,    :friends                            # many friends
     * has 1..3, :friends                            # many friends (at least 1, at most 3)
     * has 3,    :friends                            # many friends (exactly 3)
     * has 1,    :friend,  'User'                    # one friend with the class User
     * has 3,    :friends, :through => :friendships  # many friends through the friendships relationship

@param cardinality [Integer, Range, Infinity]

      cardinality that defines the association type and constraints

@param name [Symbol]

      the name that the association will be referenced by

@param model [Model, to_str]

      the target model of the relationship

@param opts [Hash]

      an options hash

@option :through[Symbol] A association that this join should go through to form

      a many-to-many association

@option :model[Model, String] The name of the class to associate with, if omitted

      then the association name is assumed to match the class name

@option :repository[Symbol]

      name of child model repository

@return [Association::Relationship] the relationship that was

      created to reflect either a one-to-one, one-to-many or many-to-many
      relationship

@raise [ArgumentError] if the cardinality was not understood. Should be a

      Integer, Range or Infinity(n)

@api public

inherited(model)

When DataMapper model is inherited, relationships of parent are duplicated and copied to subclass model

@api private

n()

Used to express unlimited cardinality of association, see `has`

@api public

relationships(repository_name = default_repository_name)

Returns copy of relationships set in given repository.

@param [Symbol] repository_name

      Name of the repository for which relationships set is returned

@return [Mash] relationships set for given repository

@api semipublic

[Validate]