Strategic loading for many-to-many relationships in Datamapper?

I am using DataMapper , an open source ORM for ruby, and I have an itch that I would like to scratch. At the moment, DataMapper can use strategic bootstrapping (SEL) for a one-to-many relationship, but not many to many, where there are N + 1 queries. I would like to hack this work correctly, but I cannot find where to do it. So, two questions:

  • How to run a test package so that it shows that this is an error (at present, all specifications that should fail are marked as pending)?
  • Where and how is SEL applied to a one-to-many relationship?
+7
ruby datamapper eager-loading
source share
1 answer

In the second question, you can try to dive into the 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/association/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 
0
source share

All Articles