目前在我的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
这会按计划进行,还是有正确的方法来做到这一点?
答案 0 :(得分:4)
我们在模型上覆盖#default_repository_name
方法来执行此操作:
class Comment
include DataMapper::Resource
def self.default_repository_name
:comments
end
end