7

I'm using DataMapper, an open source ORM for ruby, and I have in itch I would like to scratch. At the moment, DataMapper can use Strategic Eager Loading(SEL) for one-to-many relationships, but not many-to-many, where N+1 queries occur. I would like to hack around with making this work correctly, but I cannot find where to do it. So two part question:

  1. How to I run the test suite so it will show this to be failing (nb. right now all the specs that should be failing are marked as pending)?
  2. Where and how is SEL implemented for one-to-many relationships?
John F. Miller
  • 25,556
  • 8
  • 66
  • 120

1 Answers1

0

For second question, you could try dive into code:

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

  # Eager load the collection using the source as a base
  #
  # @param [Collection] source
  #   the source collection to query with
  # @param [Query, Hash] query
  #   optional query to restrict the collection
  #
  # @return [Collection]
  #   the loaded collection for the source
  #
  # @api private
  def eager_load(source, query = nil)
    targets = source.model.all(query_for(source, query))

    # FIXME: cannot associate targets to m:m collection yet
    if source.loaded? && !source.kind_of?(ManyToMany::Collection)
      associate_targets(source, targets)
    end

    targets
  end

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

    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
CodeGroover
  • 2,049
  • 17
  • 24