跨不同存储库链接数据映射关系

时间:2011-04-20 21:51:15

标签: ruby datamapper

class A
    include DataMapper::Resource

    def self.default_repository_name
        :alt_db
    end

    property :aid, Integer, :key => true
    # other stuff

    belongs_to :b, :model => 'B', :child_key => [ :bid ]

end

class B
    include DataMapper::Resource

    # this one is in the default repo

    property :bid, Integer, :key => true
    # other stuff

    belongs_to :c, :model => 'C', :child_key => [ :cid ]
end

class C
    include DataMapper::Resource

    # this one is in the default repo

    property :cid, Integer, :key => true
    # other stuff
end

如果我只有A和B,这很好用。但是,如果我添加C,则会收到错误:

dm-core / model / property.rb:73:in"new':错误的参数个数(4个为3)(ArgumentError)

如果我想与DataMapper建立一系列关系,那么我可以在一个地方提供一个ID,并通过对后续表的主键ID的一系列引用获得一个数据,例如四个表。领域,我该怎么做?

编辑:从堆栈跟踪中挖掘DM源:

DataMapper.repository(other_repository_name) do
    properties << klass.new(self, name, options, type)
end

这就是引发错误的地方。实际上,在这种情况下,klass是一个DataMapper Integer属性,它的initialize方法只接受三个选项(模型,名称和选项哈希)。

这整个块只是执行,因为我使用了多个存储库,虽然B和C在同一个存储库中,所以我不知道是否能解释为什么它在cid属性上出错。

EDIT2:

我尝试了所有的排列,看来当你进行链接时,一旦你越过数据库边界,那一定是链的末尾。例如,因为A是:alt_db而B是:default,无论C是:default,:alt_db还是第三个选项,B都尽可能深。

如果A和B都是:默认,或两者都是:alt_db,然后C是相反的,C就会尽可能深。

但我真的不明白这种行为。

3 个答案:

答案 0 :(得分:1)

你发现了一个错误。它已在master中修复。您可以尝试从git中获取源代码并查看它是否有效。

答案 1 :(得分:0)

您的代码适用于我。

irb(main):001:0> A.first.b.c
  DEBUG - "(0.001168) SELECT "aid", "bid" FROM "as" ORDER BY "aid" LIMIT 1"
  DEBUG - "(0.000337) SELECT "bid", "cid" FROM "bs" WHERE "bid" = 2 LIMIT 1"
  DEBUG - "(0.000046) SELECT "cid" FROM "cs" WHERE "cid" = 3 LIMIT 1"
=> #<C @cid=3>

我的宝石是dm-core-1.1.0,你应该查看你的版本。

答案 2 :(得分:0)

事实证明,这是DataMapper在存储库之间进行链接的一个小问题。提交给他们,据说已经修好了!

http://datamapper.lighthouseapp.com/projects/20609/tickets/1506-can-only-chain-up-to-first-time-changing-default-repository#ticket-1506-1

相关问题