2012-05-21 25 views
3

目前在我屈+ DataMapper的應用程序,我有:如何在DataMapper中將終結分爲不同的數據庫?

require 'data_mapper' 

DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/main.db") 
DataMapper.setup(:comments, "sqlite3://#{Dir.pwd}/comments.db") 

class Recording 
    include DataMapper::Resource 

    # ... 

    belongs_to :user 
    has n, :comments 
end 

class User 
    include DataMapper::Resource 

    # ... 

    has n, :recordings 
end 

class Audience 
    include DataMapper::Resource 

    # ... 
end 

# -------- ITS OWN DATABASE -------- 
class Comment 
    include DataMapper::Resource 

    #... 

    belongs_to :recording 
end 

我要評論類從別人分開進入comments.db。我環顧四周,我看到這樣的事情(和我已經格式化我的情況):

# -------- ITS OWN DATABASE -------- 
repository(:comments) do 
    class Comment 
     include DataMapper::Resource 

     #... 

     belongs_to :recording 
    end 
end 

將這項工作按計劃進行,還是有這樣做一個適當的方式?

回答

4

我們重寫我們的模型#default_repository_name方法來做到這一點:

class Comment 
    include DataMapper::Resource 

    def self.default_repository_name 
    :comments 
    end 
end 
+0

而之前'DataMapper.finalize我會做到這一點; DataMapper.auto_migrate!'? – Imnotanerd

+0

嗯,它實際上是硬編碼到你的模型中,所以是的,在這兩件事情之前。 – d11wtq

相關問題