首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Datamapper中为多到多关系加载策略?

问在Datamapper中为多到多关系加载策略?
EN

Stack Overflow用户
提问于 2009-08-26 19:14:28
回答 1查看 806关注 0票数 7

我正在使用DataMapper,这是一个用于ruby的开源ORM,而且我很想抓痒。目前,DataMapper可以为一对多的关系(但不是多对多的关系,其中发生N+1查询)使用战略热切加载( Strategic,SEL)。我想把这件事做得恰到好处,但我找不到该在哪里做。所以有两个部分的问题:

  1. 如何运行测试套件,以便它显示这是失败的(nb )。现在,所有应该失败的规范都被标记为挂起)?
  2. SEL在哪里以及如何实现一对多的关系?
EN

回答 1

Stack Overflow用户

发布于 2012-03-06 08:13:47

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

/lib/dm-核心/协会/关系.lib

代码语言:javascript
复制
  # 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:

代码语言:javascript
复制
    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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1336880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档