gpt4 book ai didi

ruby - 如何在 DataMapper 中将定稿分离到不同的数据库中?

转载 作者:数据小太阳 更新时间:2023-10-29 07:57:00 26 4
gpt4 key购买 nike

目前在我的 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

这会按计划进行吗,或者是否有适当的方法来做到这一点?

最佳答案

我们重写我们模型上的 #default_repository_name 方法来做到这一点:

class Comment
include DataMapper::Resource

def self.default_repository_name
:comments
end
end

关于ruby - 如何在 DataMapper 中将定稿分离到不同的数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10693346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com