views:

48

answers:

0

Table is a Mongoid model that must dinamically map to different databases/tables

# app/models/table.rb
class Table
  include Mongoid::Document
end

# in app/controllers/some_controller.rb
def index
   Table.connection.database = :other_database # <- How to do this ???
   Table.table_name = params[:id] # <- How to do this ???
   @records = Table.all
end

I want the Table class to:

  1. be configured per-request to different databases (on the same mongodb server connection) depending on the current logged in user
  2. same for table name

EDIT
I know about:

 Mongoid.configure do |config|
   name = "control_development"
   host = "localhost"
   config.master = Mongo::Connection.new.db(name)
   config.slaves = [
  Mongo::Connection.new(host, 27018, :slave_ok => true).db(name)
   ]
   config.persist_in_safe_mode = false
 end

BUT, does it work for certain models (?) :

  # like this i mean
  class User
  include Mongoid::Document

  configure do |config| # configure only this model's connection
    name = "other_control_development"
    host = "localhost"
    config.master = Mongo::Connection.new.db(name)
    config.slaves = [
            Mongo::Connection.new(host, 27018, :slave_ok => true).db(name)
    ]
    config.persist_in_safe_mode = false
  end

 end