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 に答える