我尝试了一些在Stack Overflow上找到的连接两个数据库的Rails方法,但是它们都没有起作用。目前我所拥有的如下:
在database.yml文件中有两个连接设置:
然后我在Rails应用程序中有很多常规模型,但也有一些需要连接到其他数据库的特殊模型,以下是我的设置方法...
在models文件夹中有一个名为static_table.rb的模块,其中包含以下内容:
那么需要其他表的特殊模型有以下内容:
然而,如果您在控制器中调用ContentItem.all,则会显示“content_items”表不存在,并且数据库连接显示为“blerg”,而不是应该是“blerg-static-api”。
非常感谢您的帮助。
在database.yml文件中有两个连接设置:
development:
adapter: postgresql
host: localhost
database: blerg
username: postgres
encoding: utf8
production:
blah...
test: &test
blah...
cucumber:
<<: *test
static_api_development:
adapter: postgresql
host: localhost
database: blerg-static-api
username: postgres
encoding: utf8
static_api_production:
blah...
static_api_test:
blah...
然后我在Rails应用程序中有很多常规模型,但也有一些需要连接到其他数据库的特殊模型,以下是我的设置方法...
在models文件夹中有一个名为static_table.rb的模块,其中包含以下内容:
class StaticTable < ActiveRecord::Base
self.abstract_class = true
establish_connection "static_api_#{Rails.env}"
end
那么需要其他表的特殊模型有以下内容:
class ContentItem < StaticTable
self.table_name = 'content_items'
end
然而,如果您在控制器中调用ContentItem.all,则会显示“content_items”表不存在,并且数据库连接显示为“blerg”,而不是应该是“blerg-static-api”。
非常感谢您的帮助。