How to separate finalization in different databases in DataMapper?

Currently in my Sinatra + DataMapper app, I have:

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

I want the comment class to be posted separately from the others in the .db comment. I looked around and I saw something similar (and to which I formatted my situation):

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

        #...

        belongs_to :recording
    end
end

Will it work as planned, or is there a way to do this?

+3
source share
1 answer

We redefine the method #default_repository_namefor our models:

class Comment
  include DataMapper::Resource

  def self.default_repository_name
    :comments
  end
end
+4
source

All Articles