简体   繁体   English

rails 3 Establishment_connection对切换数据库不起作用

[英]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.

相关问题 establish_connection似乎不支持连接 - establish_connection doesn't seem to support joins 建立连接不会带来模型关联 - establish_connection doesn't bring over model associations Rails-建立连接和嵌套表单 - Rails - establish_connection and nested forms 测试和建立连接 - testing and establish_connection 无法打开rails console:生成数据库未配置,establish_connection引发ActiveRecord :: AdapterNotSpecified - Can't open rails console: production database not configured, establish_connection raises ActiveRecord::AdapterNotSpecified 用rails查询外部数据库。 抽象类+建立连接? - Query external database with rails. Abstract Class + establish_connection? Rails 6 和 Puma 是否仍需要在工作启动时建立连接? - Is establish_connection on worker boot still required on Rails 6 and Puma? 如何在Rails中与多个数据库并行建立连接? - How to establish_connection with more than one database in parallel in Rails? Rails无法使用ActiveRecord :: establish_connection连接到数据库 - Rails cant connect to database with ActiveRecord::establish_connection 您可以使用Establishment_connection在Rails中连接到SQL视图吗 - Can you use establish_connection to connect to an SQL view in rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM