简体   繁体   English

ActiveRecord每个请求一个连接

[英]ActiveRecord one connection per request

I have an application that let the user to connect in multiples environments. 我有一个允许用户在多个环境中连接的应用程序。 Depending of the choice of environment that the user pick, activerecord will use Database X or Y. 根据用户选择的环境选择,activerecord将使用数据库X或Y。

But when I use ActiveRecord::Base.establish_connection, activerecord change the database for all users. 但是,当我使用ActiveRecord :: Base。Establishment_connection时,activerecord会更改所有用户的数据库。 I want that user 'A' use Database 'X' and user 'B' use Database 'Y' at the same time in one single application. 我希望用户“ A”在一个应用程序中同时使用数据库“ X”和用户“ B”同时使用数据库“ Y”。 Is that possible with rails? 铁轨有可能吗?

I am thinking that you can do something like this 我在想你可以做这样的事情

you do need to declare the connection in your /config/database.rb 您确实需要在/config/database.rb中声明连接

MyModel < ActiveRecord::Base
    establish_connection "other_#{session[:user_db]}"
end

You can try this answer. 您可以尝试答案。

Based on user type (X or Y), you can establish the respective DB connection. 根据用户类型(X或Y),您可以建立相应的DB连接。

Or, 要么,

You can try this gem. 你可以试试这个宝石。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在ActiveRecord(或Mongoid)中为数据库连接和table_name配置线程安全的每个请求配置吗? - Can I have thread safe per-request configuration for database connection and table_name in ActiveRecord (or Mongoid)? ActiveRecord - 在一个连接中工作 - ActiveRecord - working within one connection ActiveRecord是否按模型建立新的数据库连接 - Does ActiveRecord make a new database connection per model 有没有一种方法可以确保ActiveRecord层次结构中的每个记录引用一个对象? - Is there a way to ensure one object reference per record in an ActiveRecord hierarchy? 如何使用一个 ActiveRecord 连接截断 n 个表 - How to truncate n tables with one single ActiveRecord connection Rails - 基于用户凭据的每个请求的数据库连接 - Rails - A database connection per request, based on user credentials 将具有has_one关联的Activerecord结果转换为每个记录一个哈希 - Convert a Activerecord result with has_one associations into a single hash per record Ruby - ActiveRecord - Select 基于特定列值的每个“组”一条记录 - Ruby - ActiveRecord - Select one record per 'group' based on a specific column value Rails ActiveRecord连接会话 - Rails ActiveRecord connection session Managine ActiveRecord连接池 - Managine ActiveRecord connection pools
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM