Strategic Eager Loading for many-to-many relations in Datamapper?
Asked Answered
G

1

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?
Gers answered 26/8, 2009 at 19:14 Comment(0)
D
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
Dingess answered 6/3, 2012 at 8:13 Comment(2)
Wow, 2½ years and this still is a FIXME! Do the tests at least fail correctly now?Gers
Actually, i'm not developing with DataMapper, i wish i could but have no time. Maybe this question can be asked in DataMapper groups or IRC.Dingess

© 2022 - 2024 — McMap. All rights reserved.