【问题标题】:How do I separate finalizing into different databases in DataMapper?如何在 DataMapper 中将 finalizing 分离到不同的数据库中?
【发布时间】:2012-05-28 10:21:12
【问题描述】:

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

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

        #...

        belongs_to :recording
    end
end

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

【问题讨论】:

    标签: ruby sinatra datamapper ruby-datamapper


    【解决方案1】:

    我们在我们的模型上覆盖 #default_repository_name 方法来做到这一点:

    class Comment
      include DataMapper::Resource
    
      def self.default_repository_name
        :comments
      end
    end
    

    【讨论】:

    • 我会在DataMapper.finalize ; DataMapper.auto_migrate!之前这样做?
    • 嗯,它实际上是硬编码到你的模型中的,所以是的,在这两件事之前。
    猜你喜欢
    • 2021-12-23
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 2012-09-15
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多