可以在rails应用上使用两个sqlite3数据库吗?
问题描述:
在我的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分贝?
如果是这样,我该如何解决这个问题。
答
是的,当然,但在每个模型中,您应该定义要使用的数据库。 您可以使用方法establish_connection来定义哪个数据库应该使用当前模型。 更多这里http://api.rubyonrails.org/classes/ActiveRecord/Base.html。但我可以推荐创建几个基本活动记录类来定义每个数据库的名称,然后从它们继承你的模型类。
看一看这个问题: 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
你试图寻找解决方案的根本原因是什么? – 2013-04-04 21:09:03