战略渴望加载Datamapper中的多对多关系?

Joh*_*ler 7 ruby datamapper eager-loading

我正在使用DataMapper,一个用于ruby的开源ORM,而且我已经知道了.目前,DataMapper可以将战略预测加载(SEL)用于一对多关系,但不能使用多对多关系,其中发生N + 1个查询.我想破解正确的工作,但我找不到在哪里做.所以两部分问题:

  1. 如何运行测试套件,以便它显示失败(nb.现在所有应该失败的规格被标记为待定)?
  2. SEL为何以及如何实现一对多关系?

Cod*_*ver 0

对于第二个问题,您可以尝试深入研究代码:

/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
Run Code Online (Sandbox Code Playgroud)

./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
Run Code Online (Sandbox Code Playgroud)