2011-04-20 82 views
0
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-核心/模型/ property.rb:73:在`新':錯誤的參數數目(4 3)(引發ArgumentError)

如果我想與DataMapper建立一系列關係,這樣我就可以在一個地方給出一個ID,並通過一系列對後續表的主鍵ID字段的引用來獲得一張數據,比如說,有四張表。我這樣做?

編輯:挖掘到從堆棧跟蹤的DM來源:

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

這也正是引發錯誤。事實上,在這種情況下,klass是DataMapper Integer屬性,它的初始化方法只接受三個選項(模型,名稱和選項散列)。

這整個塊只執行,因爲我使用多個存儲庫,雖然B和C在同一個存儲庫,所以我不知道這是否揭示了爲什麼它在cid屬性上出現錯誤。

EDIT2:

我已經嘗試了所有的排列,看來,當你鏈接,一旦你跨越數據庫邊界,即必須是鏈的末端。例如,由於A是:alt_db,B是:默認值,所以B的深度就像我可以走的一樣深,不管C是:default,:alt_db還是第三個選項。

如果相反,A和B都是:默認的,或者兩者都是:alt_db,然後C是相反的,C會盡可能深。

雖然我不明白這種行爲。

回答

1

實際上您發現了一個錯誤。它已被固定在主。你可以嘗試從git中獲取源代碼,看看它是否有效。

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,你應該檢查你的版本。

+0

這很奇怪...我有相同的版本,它不工作.... – hsiu 2011-04-21 14:09:53