[英]rails 3 establish_connection doesn't work for switching databases
I need to use two different db in my app. 我需要在我的应用程序中使用两个不同的数据库。 I found a simple example: 我找到了一个简单的例子:
http://pullmonkey.com/2008/4/21/ruby-on-rails-multiple-database-connections/ http://pullmonkey.com/2008/4/21/ruby-on-rails-multiple-database-connections/
using establish_connection, but it doesn't seem to work. 使用Establishment_connection,但似乎不起作用。 To test switching databases, I have: 为了测试交换数据库,我有:
class User < ActiveRecord::Base
establish_connection :test
# attr_accessible :title, :body
end
But I get the error: "Uncaught exception: database configuration does not specify adapter". 但我收到错误消息:“未捕获的异常:数据库配置未指定适配器”。
Here is my database.yaml: 这是我的database.yaml:
development:
adapter: mysql2
encoding: utf8
database: foo
username: bar
host: foo.com
password: foobar
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I am running in development mode for this test. 我正在以开发模式运行此测试。 Any ideas why it can't switch? 有什么想法为什么它不能切换? Thanks. 谢谢。
First the "pulmonkeyexample" is to use a database label when you have multiple databases and other than the three standard ones - "development, test and production". 首先,“ pulmonkeyexample”是在您拥有多个数据库以及三个标准数据库(“开发,测试和生产”)以外的数据库时使用数据库标签。 When you start a rails application, you can use "-e" option to tell rails which mode to start the application in - test, dev or prod. 启动Rails应用程序时,可以使用“ -e”选项告诉Rails以哪种模式启动应用程序-测试,开发或生产。 Depending on the mode in which the app is starting, Rails will automatically use corresponding database connections from the "database.yml" file. 根据应用程序启动的模式,Rails将自动使用“ database.yml”文件中的相应数据库连接。 So remove that line from your model. 因此,从模型中删除该行。 On second thoughts, even if you write yet it should work provided there is a test database with the user table. 再三考虑,即使您编写了它也应该可以工作,只要有一个包含用户表的测试数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.