简体   繁体   English

基于用户参数的rails中的不同数据库连接

[英]Different database connections in rails based on user params

I'm refactoring some features of a legacy php application which uses multiple databases with the same structure, one per language. 我正在重构遗留php应用程序的一些功能,它使用具有相同结构的多个数据库,每种语言一个。 When an user login choose his language and then all the following connections are made with the db for that app with some code like this: 当用户登录选择他的语言,然后所有以下连接都是使用该应用程序的数据库进行的,其代码如下:

$db_name = 'db_appname_' . $_SESSION['language'];
mysql_connect(...);
mysql_select_db($db_name);

I'd like to refactor also the database, but currently it's not an option because other pieces of software should remain in production with the old structure while the new app is developed, and for some time after it's been developed. 我想重构数据库,但目前它不是一个选项,因为其他软件应该在新应用程序开发的同时保留生产旧结构,并且在开发之后的一段时间内。

I saw this question , but both the question and the suggested gems are pretty old and it seems that they are not working with Rails 3. 我看到了这个问题 ,但问题和建议的宝石都很老了,似乎他们没有使用Rails 3。

Which is the best method to achieve this behavior in my new rails 3 app? 哪个是在我的新rails 3应用程序中实现此行为的最佳方法? Is there any other choice that avoid me to alter the db structure and fits my needs? 还有其他选择可以避免我改变数据库结构并满足我的需求吗?

Last detail: in the php app even the login information are kept in separate tables, ie every db has its own users table and when the user logs in it also passes a language param in the login form. 最后的细节:在php应用程序中,甚至登录信息都保存在单独的表中,即每个数据库都有自己的用户表,当用户登录时,它也会在登录表单中传递一个语言参数。 I'd like to use devise for auth in the new app which likely won't work with this approach, so I'm thinking to duplicate (I know, this is not DRY) login information in a separate User model with a language attribute and a separate db shared among languages to use devise features with my app. 我想在新应用程序中使用devise for auth,这可能不适用于这种方法,所以我想在具有语言属性的单独用户模型中复制(我知道,这不是DRY)登录信息和一个单独的数据库共享语言,以便在我的应用程序中使用设计功能。 Will this cause any issue? 这会引起任何问题吗?

EDIT: 编辑:

For completeness, I ended with this yml configuration file 为了完整起见,我结束了这个yml配置文件

production: &production
  adapter: mysql
  host: localhost
  username: user
  password: secret
  timeout: 5000

production_italian:
  <<: *production
  database: db_app_ita

production_english:
  <<: *production
  database: db_app_eng

and with this config in base model (actually not exactly this but this is for keeping things clear) 并在基本模型中使用此配置(实际上不是这个,但这是为了保持清晰)

MyModel < AR::Base
  establish_connection "production_#{session[:language]}"
  ...
end

use establish_connection in your models: 在模型中使用establish_connection

MyModel < AR::Base
  establish_connection "db_appname_#{session[:language]}"
  ...
end

Use MultiConfig gem I created to make this easy. 使用我创建的MultiConfig gem来简化这一过程。

You could specify the configs in separate file like database_italian.yml etc and then call: 您可以在单独的文件中指定配置,如database_italian.yml等,然后调用:

ActiveRecord::Base.config_file = 'database_italian'

This way it will be much easier to maintain and cleaner looking. 这样,维护和清洁看起来会更容易。 Just add more language db config files as you wish 只需添加更多语言db配置文件即可

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM