如何在Rails应用程序中使用多个数据库使用database.yml [英] How to Use Multiple Databases in a Rails App Using the database.yml
问题描述
我已阅读有关如何执行此操作的文档,但在实践中,我遇到了问题。
在我的应用程序中,我有两个不同的数据库,如下所述在我的database.yml文件。
sqlite_test:
adapter:sqlite3
database:db / sqlite_test.sqlite3
table: plots
pool:5
timeout:5000
开发:
适配器:mysql2
编码:utf8
reconnect:false
database:test
pool:5
username:myname
password:mypassword
host:localhost
我的应用程序是一个动态绘图仪,它将绘制(基本)数据库中的数据,而无需了解数据库中的内容或其结构。这两个数据库都包含不同的数据。我在一个单独的Rails应用程序中创建的SQLite数据库。
我使用的当前应用程序是围绕MYSQL数据库构建的。
我将SQLite数据库复制到/ db目录中。所以在我的主模型中,当我说:
class Plot< ActiveRecord :: Base
establish_connection:development
set_table_namestock_test
set_primary_key:id
一切正常,只是罚款和dandy。但是,当我将其更改为:
establish_connection:sqlite_test
set_table_nameplots
并尝试通过Rails控制台访问该数据库,我得到一个错误:
>> ActiveRecord :: AdapterNotSpecified:数据库配置未指定适配器
我不知道为什么,因为database.yml文件清楚地指定一个适配器?
当我在我的模型手工做,但一切工作正如它应该。
class Plot< ActiveRecord :: Base
establish_connection(:adapter =>sqlite3,:database =>db / sqlite_test.sqlite3,:pool => 5)
当我手动指定database.yml中的内容时,而不是当我只是使用database.yml引用时,为什么全部工作?
谢谢!
解决方案我试图复制并让它工作。它与命名约定有关:
您处于开发模式,AR将寻找开发标记,在您的情况下,sqlite不存在。
这是我的database.yml:
开发:
adapter:mysql2
数据库:se_development
username:root
pool:5
timeout:5000
sqlite_development:
adapter:sqlite3
database:db / development.sqlite3
pool:5
timeout:5000
以下是型号:
class Plot< ActiveRecord :: Base
establish_connection'sqlite_'+ Rails.env
end
为我工作。希望这有帮助。
I've read up on the documentation on how to do this, but in practice, I am having problems. In my app, I have 2 different databases as described below in my database.yml file.
sqlite_test: adapter: sqlite3 database: db/sqlite_test.sqlite3 table: plots pool: 5 timeout: 5000 development: adapter: mysql2 encoding: utf8 reconnect: false database: test pool: 5 username: myname password: mypassword host: localhost
My application is a dynamic plotter that will plot the data in a (basic) database without having knowledge of whats in the database, or how its structured. Both of these databases contain different data. The SQLite database I created in a separate Rails app.
The current app I'm using is built around the MYSQL database, which I build externally. I copied the SQLite database into the /db directory. So in my main model, when I say:
class Plot < ActiveRecord::Base establish_connection :development set_table_name "stock_test" set_primary_key :id
Everything works out just fine and dandy. However, when I change it to:
establish_connection :sqlite_test set_table_name "plots"
and try to access that database via the Rails console, I get an error saying:
>>ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter
I don't know why that is, since the database.yml file clearly does specify an adapter? When I do it by hand in my model though, everything works exactly as it should.
class Plot < ActiveRecord::Base establish_connection(:adapter => "sqlite3", :database => "db/sqlite_test.sqlite3", :pool => 5 )
Why does it all work when I manually specify whats in the database.yml, but not when I just use the database.yml reference?
Thanks!
解决方案I tried to replicate and got it to work. It has to do with naming conventions: You're in development mode and AR will look for the development tag, which in your case does not exist for sqlite.
Here is my database.yml:
development: adapter: mysql2 database: se_development username: root pool: 5 timeout: 5000 sqlite_development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000
Here is the model:
class Plot < ActiveRecord::Base establish_connection 'sqlite_' + Rails.env end
Worked for me. Hope this helps.
这篇关于如何在Rails应用程序中使用多个数据库使用database.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!