表是一个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

最佳答案

您可以使用此连接到多个数据库。
示例配置:
https://github.com/mongoid/mongoid/blob/master/spec/config/mongoid_with_multiple_mongos.yml
在您的模型中:

set_database :secondary

当前无法在运行时按所需方式交换数据库。不过,这已经在待办事项清单上了,所以请密切关注。

关于ruby-on-rails - Mongoid中的ActiveRecord#Establishment_connection等效项是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3798003/

10-15 12:11