テーブルは、異なるデータベース/テーブルに動的にマップする必要がある Mongoid モデルです
# 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
私はテーブルクラスに次のことを望みます:
- 現在ログインしているユーザーに応じて、(同じmongodbサーバー接続で)異なるデータベースへのリクエストごとに構成されます
- テーブル名も同じ
私が知っている編集:
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
しかし、それは特定のモデルで機能しますか (?) :
# 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