Sha256: c0b68a8473e92622a370ef657dd22b35559c392d4f8b0652dbf5f797f20fa8ae

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Associations #:nodoc:
    class RelatesToMany < DelegateClass(Array) #:nodoc:

      # Initializing a related association only requires looking up the objects
      # by their ids.
      #
      # Options:
      #
      # document: The +Document+ that contains the relationship.
      # options: The association +Options+.
      def initialize(document, options)
        name = document.class.to_s.foreign_key
        @documents = options.klass.all(:conditions => { name => document.id })
        super(@documents)
      end

      class << self
        # Preferred method for creating the new +RelatesToMany+ association.
        #
        # Options:
        #
        # document: The +Document+ that contains the relationship.
        # options: The association +Options+.
        def instantiate(document, options)
          new(document, options)
        end

        # Returns the macro used to create the association.
        def macro
          :relates_to_many
        end

        # Perform an update of the relationship of the parent and child. This
        # will assimilate the child +Document+ into the parent's object graph.
        #
        # Options:
        #
        # related: The related object
        # parent: The parent +Document+ to update.
        # options: The association +Options+
        #
        # Example:
        #
        # <tt>RelatesToOne.update(game, person, options)</tt>
        def update(related, document, options)
          name = document.class.to_s.underscore
          related.each { |child| child.send("#{name}=", document) }
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-0.9.8 lib/mongoid/associations/relates_to_many.rb
mongoid-0.9.7 lib/mongoid/associations/relates_to_many.rb