代码之家  ›  专栏  ›  技术社区  ›  clyfe

什么是ActiveRecord在MongoID中建立连接的等价物?

  •  2
  • clyfe  · 技术社区  · 14 年前

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

    我希望桌面类:

    1. 根据当前登录用户对不同数据库(在同一MongoDB服务器连接上)的每个请求进行配置
    2. 表名相同

    编辑
    我知道:

     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
    
    1 回复  |  直到 13 年前