Class: DataMapper::Associations::ManyToMany::Relationship

### Home Classes Methods

### In Files

- lib/dm-core/associations/many_to_many.rb

### Parent

DataMapper::Associations::OneToMany::Relationship

### Methods

- #child_key
- #eager_load
- #finalize
- #links
- #query
- #source_scope
- #target_key
- #through
- #via

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::Adapters
- DataMapper::Adapters::AbstractAdapter
- DataMapper::Adapters::InMemoryAdapter
- DataMapper::Assertions
- DataMapper::Associations
- DataMapper::Associations::ManyToMany
- DataMapper::Associations::ManyToMany::Collection
- DataMapper::Associations::ManyToMany::Relationship
- DataMapper::Associations::ManyToOne
- DataMapper::Associations::ManyToOne::Relationship
- DataMapper::Associations::OneToMany
- DataMapper::Associations::OneToMany::Collection
- DataMapper::Associations::OneToMany::Relationship
- DataMapper::Associations::OneToOne
- DataMapper::Associations::OneToOne::Relationship
- DataMapper::Associations::Relationship
- DataMapper::Chainable
- DataMapper::Collection
- DataMapper::Deprecate
- DataMapper::DescendantSet
- DataMapper::Equalizer
- DataMapper::Ext
- DataMapper::Ext::Array
- DataMapper::Ext::Hash
- DataMapper::Ext::Module
- DataMapper::Ext::Object
- DataMapper::Ext::String
- DataMapper::Hook
- DataMapper::Hook::ClassMethods
- DataMapper::IdentityMap
- DataMapper::ImmutableDeletedError
- DataMapper::ImmutableError
- DataMapper::IncompleteModelError
- DataMapper::Inflector
- DataMapper::Inflector::Inflections
- DataMapper::LocalObjectSpace
- DataMapper::Logger
- DataMapper::Mash
- DataMapper::Model
- DataMapper::Model::Hook
- DataMapper::Model::Hook::MethodCommand
- DataMapper::Model::Hook::Methods
- DataMapper::Model::Hook::ProcCommand
- DataMapper::Model::Is
- DataMapper::Model::Property
- DataMapper::Model::Relationship
- DataMapper::Model::Scope
- DataMapper::NamingConventions
- DataMapper::NamingConventions::Field
- DataMapper::NamingConventions::Field::Underscored
- DataMapper::NamingConventions::Field::UnderscoredAndPluralized
- DataMapper::NamingConventions::Field::UnderscoredAndPluralizedWithoutModule
- DataMapper::NamingConventions::Field::Yaml
- DataMapper::NamingConventions::Resource
- DataMapper::NamingConventions::Resource::Underscored
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralized
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutLeadingModule
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutModule
- DataMapper::NamingConventions::Resource::Yaml
- DataMapper::ObjectNotFoundError
- DataMapper::OrderedSet
- DataMapper::OrderedSet::Cache
- DataMapper::OrderedSet::Cache::API
- DataMapper::PersistenceError
- DataMapper::PluginNotFoundError
- DataMapper::Property
- DataMapper::Property::Binary
- DataMapper::Property::Boolean
- DataMapper::Property::Class
- DataMapper::Property::Date
- DataMapper::Property::DateTime
- DataMapper::Property::Decimal
- DataMapper::Property::Discriminator
- DataMapper::Property::Discriminator::Model
- DataMapper::Property::Float
- DataMapper::Property::Integer
- DataMapper::Property::Lookup
- DataMapper::Property::Numeric
- DataMapper::Property::Object
- DataMapper::Property::PassThroughLoadDump
- DataMapper::Property::Serial
- DataMapper::Property::String
- DataMapper::Property::Text
- DataMapper::Property::Time
- DataMapper::Property::Typecast
- DataMapper::Property::Typecast::Numeric
- DataMapper::Property::Typecast::Time
- DataMapper::PropertySet
- DataMapper::Query
- DataMapper::Query::Conditions
- DataMapper::Query::Conditions::AbstractComparison
- DataMapper::Query::Conditions::AbstractOperation
- DataMapper::Query::Conditions::AndOperation
- DataMapper::Query::Conditions::Comparison
- DataMapper::Query::Conditions::EqualToComparison
- DataMapper::Query::Conditions::FlattenOperation
- DataMapper::Query::Conditions::GreaterThanComparison
- DataMapper::Query::Conditions::GreaterThanOrEqualToComparison
- DataMapper::Query::Conditions::InclusionComparison
- DataMapper::Query::Conditions::LessThanComparison
- DataMapper::Query::Conditions::LessThanOrEqualToComparison
- DataMapper::Query::Conditions::LikeComparison
- DataMapper::Query::Conditions::NotOperation
- DataMapper::Query::Conditions::NullOperation
- DataMapper::Query::Conditions::Operation
- DataMapper::Query::Conditions::OrOperation
- DataMapper::Query::Conditions::RegexpComparison
- DataMapper::Query::Conditions::RelationshipHandler
- DataMapper::Query::Direction
- DataMapper::Query::Operator
- DataMapper::Query::Path
- DataMapper::Query::Sort
- DataMapper::RelationshipSet
- DataMapper::Repository
- DataMapper::RepositoryNotSetupError
- DataMapper::Resource
- DataMapper::Resource::PersistenceState
- DataMapper::Resource::PersistenceState::Clean
- DataMapper::Resource::PersistenceState::Deleted
- DataMapper::Resource::PersistenceState::Dirty
- DataMapper::Resource::PersistenceState::Immutable
- DataMapper::Resource::PersistenceState::Persisted
- DataMapper::Resource::PersistenceState::Transient
- DataMapper::SaveFailureError
- DataMapper::Spec
- DataMapper::Spec::Adapters
- DataMapper::Spec::Adapters::Adapter
- DataMapper::Spec::Adapters::Helpers
- DataMapper::Spec::Adapters::InMemoryAdapter
- DataMapper::Spec::CollectionHelpers
- DataMapper::Spec::CollectionHelpers::GroupMethods
- DataMapper::Spec::Helpers
- DataMapper::Spec::PendingHelpers
- DataMapper::Subject
- DataMapper::SubjectSet
- DataMapper::SubjectSet::NameCache
- DataMapper::Undefined
- DataMapper::UnknownRelationshipError
- DataMapper::UnsavedParentError
- DataMapper::UpdateConflictError
- Blog
- Blog::Article
- ChildSubType
- CounterAdapter
- Heffalump
- Kernel
- LazyArray
- Object
- OtherComparison
- Pathname
- SubType
- Symbol

No matching classes.

# DataMapper::Associations::ManyToMany::Relationship

### Constants

OPTIONS

### Public Instance Methods

child_key() click to toggle source

Returns a set of keys that identify the target model

@return [DataMapper::PropertySet]

    a set of properties that identify the target model

@api semipublic

    # File lib/dm-core/associations/many_to_many.rb, line 15
    def child_key
      return @child_key if defined?(@child_key)

      repository_name = child_repository_name || parent_repository_name
      properties      = child_model.properties(repository_name)

      @child_key = if @child_properties
        child_key = properties.values_at(*@child_properties)
        properties.class.new(child_key).freeze
      else
        properties.key
      end
    end

Also aliased as: target_key
eager_load(source, other_query = nil) click to toggle source

Eager load the collection using the source as a base

@param [Resource, Collection] source

    the source to query with

@param [Query, Hash] other_query

    optional query to restrict the collection

@return [ManyToMany::Collection]

    the loaded collection for the source

@api private

    # File lib/dm-core/associations/many_to_many.rb, line 158
    def eager_load(source, other_query = nil)
      # FIXME: enable SEL for m:m relationships
      source.model.all(query_for(source, other_query))
    end

finalize() click to toggle source

Initialize the chain for "many to many" relationships

@api public

    # File lib/dm-core/associations/many_to_many.rb, line 130
    def finalize
      through
      via
    end

links() click to toggle source

@api semipublic

    # File lib/dm-core/associations/many_to_many.rb, line 110
    def links
      return @links if defined?(@links)

      @links = []
      links  = [ through, via ]

      while relationship = links.shift
        if relationship.respond_to?(:links)
          links.unshift(*relationship.links)
        else
          @links << relationship
        end
      end

      @links.freeze
    end

query() click to toggle source

@api private

    # File lib/dm-core/associations/many_to_many.rb, line 141
    def query
      # TODO: consider making this a query_for method, so that ManyToMany::Relationship#query only
      # returns the query supplied in the definition
      @many_to_many_query ||= super.merge(:links => links).freeze
    end

source_scope(source) click to toggle source

@api private

    # File lib/dm-core/associations/many_to_many.rb, line 136
    def source_scope(source)
      { through.inverse => source }
    end

target_key() click to toggle source

@api semipublic

Alias for: child_key
through() click to toggle source

Intermediate association for through model relationships

Example: for :bugs association in

class Software::Engineer

    include DataMapper::Resource

    has n, :missing_tests
    has n, :bugs, :through => :missing_tests

end

through is :missing_tests

TODO: document a case when through option is a model and not an association name

@api semipublic

    # File lib/dm-core/associations/many_to_many.rb, line 51
    def through
      return @through if defined?(@through)

      @through = options[:through]

      if @through.kind_of?(Associations::Relationship)
        return @through
      end

      model           = source_model
      repository_name = source_repository_name
      relationships   = model.relationships(repository_name)
      name            = through_relationship_name

      @through = relationships[name] ||
        DataMapper.repository(repository_name) do
          model.has(min..max, name, through_model, one_to_many_options)
        end

      @through.child_key

      @through
    end

via() click to toggle source

@api semipublic

    # File lib/dm-core/associations/many_to_many.rb, line 76
    def via
      return @via if defined?(@via)

      @via = options[:via]

      if @via.kind_of?(Associations::Relationship)
        return @via
      end

      name            = self.name
      through         = self.through
      repository_name = through.relative_target_repository_name
      through_model   = through.target_model
      relationships   = through_model.relationships(repository_name)
      singular_name   = DataMapper::Inflector.singularize(name.to_s).to_sym

      @via = relationships[@via] ||
        relationships[name]      ||
        relationships[singular_name]

      @via ||= if anonymous_through_model?
        DataMapper.repository(repository_name) do
          through_model.belongs_to(singular_name, target_model, many_to_one_options)
        end
      else
        raise UnknownRelationshipError, "No relationships named #{name} or #{singular_name} in #{through_model}"
      end

      @via.child_key

      @via
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.