2013-02-28 38 views
0

我想使用DataMapper遷移舊數據庫,並且遇到了多對多關係的問題。DataMapper - 使用匿名資源設置多對多關係的存儲庫

我有一個PostTag模型,都通過一個匿名資源。我可以在帖子和標籤模型中設置存儲庫名稱,但不能自動生成PostTag模型(據我所知)。有沒有辦法讓所有人都使用相同的存儲庫名稱(:legacy)?

乾杯,
湯姆

回答

1

您只需創建一個普通的DM模型「中」資源能夠定義庫的名稱,如

model PostTag 
    include DataMapper::Resource 
    def self.default_repository_name; :legacy end 
    belongs_to :post, :key => true 
    belongs_to :tag, :key => true 
end 

,並在這兩個的父母,用:through定義連接。例如,

model Post 
    # other definitions ... 
    has n, :post_tags 
    has n, :tags, :through => :post_tags 
end 
相關問題