2013-04-04 81 views
1

在我的config/database.yml文件可以在rails应用上使用两个sqlite3数据库吗?

# SQLite version 3.x 
# gem install sqlite3 
# 
# Ensure the SQLite 3 gem is defined in your Gemfile 
# gem 'sqlite3' 
development: 
    adapter: sqlite3 
    database: db/development.sqlite3 
    pool: 5 
    timeout: 5000 

# Warning: The database defined as "test" will be erased and 
# re-generated from your development database when you run "rake". 
# Do not set this db to the same as development or production. 
test: 
    adapter: sqlite3 
    database: db/test.sqlite3 
    pool: 5 
    timeout: 5000 

production: 
    adapter: sqlite3 
    database: db/production.sqlite3 
    pool: 5 
    timeout: 5000 

otherdb: 
    adapter: sqlite3 
    database: /c/Users/dsun/Documents/apeers/db/development.sqlite3 
    pool: 5 
    timeout: 10000 

,并在我的模型,我通过调用新的数据库:establish_connection :otherdb

但这返回一个错误,database configuration does not specify adapter

所以是问题那sqlite3不能在一次使用2分贝?

如果是这样,我该如何解决这个问题。

+0

看一看这个问题: http://stackoverflow.com/questions/10360705/how-to-use-multiple-databases-in-a-rails-app-using-the-database- yml – ply 2013-04-04 19:40:23

+0

你试图寻找解决方案的根本原因是什么? – 2013-04-04 21:09:03

回答

2

是的,当然,但在每个模型中,您应该定义要使用的数据库。 您可以使用方法establish_connection来定义哪个数据库应该使用当前模型。 更多这里http://api.rubyonrails.org/classes/ActiveRecord/Base.html。但我可以推荐创建几个基本活动记录类来定义每个数据库的名称,然后从它们继承你的模型类。

+0

非常感谢!我已经解决了这个问题〜原来的路线是在我的应用程序的路线下,所以我使用这个“../”在我的应用程序之外返回,并找到外部数据库。我创建了一个外部数据库的父模型,让我的模型按照您的建议继承,现在它正在工作! – sun 2013-04-05 14:46:16

+0

不客气! – freeze 2013-04-06 13:33:38

相关问题