如何在DataMapper中将最终化分成不同的数据库?

时间:2012-05-21 22:10:33

标签: ruby sinatra datamapper ruby-datamapper

目前在我的Sinatra + 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类与其他类分开进入comments.db。我环顾四周,我看到了这样的东西(我根据我的情况格式化了):

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

        #...

        belongs_to :recording
    end
end

这会按计划进行,还是有正确的方法来做到这一点?

1 个答案:

答案 0 :(得分:4)

我们在模型上覆盖#default_repository_name方法来执行此操作:

class Comment
  include DataMapper::Resource

  def self.default_repository_name
    :comments
  end
end