我有一个运行在Unicorn上的Sinatra应用程序,它使用Mongoid作为它的模型。我有几个结构相同但内容不同的Mongo数据库,我为每个用户在他/她登录时选择正确的数据库。我想知道Mongoid 3.0是否可以这样做。在运行时在Mongoid 3.0中切换数据库
2
A
回答
3
您可以查询每次使用前with
操作:
Model.with(database: method_to_get_the_db_name).create
2
如果你想切换数据库,使用Mongoid.override_database
,它是线程安全的。
Mongoid.override_database("client_db_name") # change the database
Mongoid.override_database(nil) # reset the database
实施例:
class ApplicationController < ActionController::Base
before_filter :switch_database
after_filter :reset_database
private
def switch_database
client_ref = params[:client_id]
Mongoid.override_database("my_db_name_#{client_ref}")
end
def reset_database
Mongoid.override_database(nil)
end
end
文档可以发现here。
相关问题
- 1. 在运行时切换TG2中的数据库
- 2. 在运行时替换SQLite数据库
- 3. 在运行时切换MasterPageFile
- 4. 在运行时切换TraceSwitch
- 5. 在Codeigniter Mongo类中切换数据库
- 6. 在LinqToSql中切换数据库
- 7. 在rails中切换数据库阶段
- 8. 切换数据库
- 9. 在运行时切换数据模板 - 刷新问题
- 10. 切换Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden在运行时
- 11. 如何在运行时切换resx?
- 12. 在运行时切换App.Config设置C#
- 13. 运行时在线程中切换运行方法
- 14. 切换数据库值
- 15. 在运行时创建数据库
- 16. 在实时数据库上运行mysqldump
- 17. 根据选定的某个值切换基于运行时间的数据库
- 18. 动态切换数据库
- 19. 在数据库之间切换Spring MongoDb
- 20. Zend在数据库之间切换
- 21. 如何在使用db.collection.insert()时即时切换MongoDB数据库?
- 22. 在运行时在ASP.Net中切换appSettings文件路径MVC
- 23. 在运行时切片
- 24. 在Report Builder 3.0中搜索数据库
- 25. 在运行时在Grails应用程序中更改数据库
- 26. 我如何在运行时在C#中创建Access数据库?
- 27. 如何在iPhone运行时在bundle中创建sqlite数据库?
- 28. PowerShell Invoke-SqlCmd切换到主数据库
- 29. Firebase数据库在服务中运行
- 30. 在C#中运行数据库脚本