Class: DataMapper::Associations::OneToMany::Relationship

### Home Classes Methods

### In Files

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

### Parent

DataMapper::Associations::Relationship

### Methods

- ::new
- #child_key
- #collection_for
- #default_for
- #finalize
- #get
- #get_collection
- #lazy_load
- #set
- #set_collection
- #target_key

### 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::OneToMany::Relationship

### Public Class Methods

new(name, target_model, source_model, options = {}) click to toggle source

@api semipublic

    # File lib/dm-core/associations/one_to_many.rb, line 127
    def initialize(name, target_model, source_model, options = {})
      target_model ||= DataMapper::Inflector.camelize(DataMapper::Inflector.singularize(name.to_s))
      options        = { :min => 0, :max => source_model.n }.update(options)
      super
    end

### Public Instance Methods

child_key() click to toggle source

@api semipublic

    # File lib/dm-core/associations/one_to_many.rb, line 21
    def child_key
      inverse.child_key
    end

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

Returns a Collection for this relationship with a given source

@param [Resource] source

    A Resource to scope the collection with

@param [Query] other_query (optional)

    A Query to further scope the collection with

@return [Collection]

    The collection scoped to the relationship, source and query

@api private

    # File lib/dm-core/associations/one_to_many.rb, line 39
    def collection_for(source, other_query = nil)
      query = query_for(source, other_query)

      collection = collection_class.new(query)
      collection.relationship = self
      collection.source       = source

      # make the collection empty if the source is new
      collection.replace([]) if source.new?

      collection
    end

default_for(source) click to toggle source

@api semipublic

    # File lib/dm-core/associations/one_to_many.rb, line 120
    def default_for(source)
      collection_for(source).replace(Array(super))
    end

finalize() click to toggle source

initialize the inverse "many to one" relationships explicitly before initializing other relationships. This makes sure that foreign key properties always appear in the order they were declared.

@api public

    # File lib/dm-core/associations/one_to_many.rb, line 108
    def finalize
      child_model.relationships.each do |relationship|
        # TODO: should this check #inverse?
        #   relationship.child_key if inverse?(relationship)
        if relationship.kind_of?(Associations::ManyToOne::Relationship)
          relationship.finalize
        end
      end
      inverse.finalize
    end

get(source, query = nil) click to toggle source

Loads and returns association targets (ex.: articles) for given source resource (ex.: author)

@api semipublic

    # File lib/dm-core/associations/one_to_many.rb, line 56
    def get(source, query = nil)
      lazy_load(source)
      collection = get_collection(source)
      query ? collection.all(query) : collection
    end

get_collection(source) click to toggle source

@api private

    # File lib/dm-core/associations/one_to_many.rb, line 63
    def get_collection(source)
      get!(source)
    end

lazy_load(source) click to toggle source

Loads association targets and sets resulting value on given source resource

@param [Resource] source

    the source resource for the association

@return [undefined]

@api private

    # File lib/dm-core/associations/one_to_many.rb, line 90
    def lazy_load(source)
      return if loaded?(source)

      # SEL: load all related resources in the source collection
      if source.saved? && (collection = source.collection).size > 1
        eager_load(collection)
      end

      unless loaded?(source)
        set!(source, collection_for(source))
      end
    end

set(source, targets) click to toggle source

Sets value of association targets (ex.: paragraphs) for given source resource (ex.: article)

@api semipublic

    # File lib/dm-core/associations/one_to_many.rb, line 71
    def set(source, targets)
      lazy_load(source)
      get!(source).replace(targets)
    end

set_collection(source, target) click to toggle source

@api private

    # File lib/dm-core/associations/one_to_many.rb, line 77
    def set_collection(source, target)
      set!(source, target)
    end

target_key() click to toggle source

@api semipublic

Alias for: child_key

[Validate]

Generated with the Darkfish Rdoc Generator 2.