3

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

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

4

1 回答 1

4

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

class Comment
  include DataMapper::Resource

  def self.default_repository_name
    :comments
  end
end
于 2012-05-22T02:50:19.827 回答