无法连接到rails 3.2中的两个postgres数据库。 [英] Cannot connect to two postgres databases in rails 3.2.

查看:135
本文介绍了无法连接到rails 3.2中的两个postgres数据库。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几个方法发现堆栈溢出连接到两个数据库在rails二,但不是他们正在工作。这是我目前的情况:

I've tried a few methods found on stack overflow for connecting to two database in rails two however non of them are working. Here's what I've got at the moment:

在database.yml中有两个连接设置:

In the database.yml there's two connection settings:

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应用程序,但也是奇特殊的模型,需要连接到其他数据库,我已经设置...

And then I have lots of normal models in the rails app, but also the odd special model that need to connect to the other database, here's how I've set it up...

在模型文件夹中有一个名为static_table.rb的模块,其中包含以下内容:

There is a module in the models folder called static_table.rb which has this content:

class StaticTable < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "static_api_#{Rails.env}"
end

然后需要其他表的特殊模型有:

Then the special models that need the other tables have this:

class ContentItem < StaticTable
  self.table_name = 'content_items'
end

ContentItem.all在控制器中表示content_items表不存在,数据库连接显示为blerg而不是blerg-static-api,它应该是。

However if you call ContentItem.all in a controller it says the 'content_items' table does not exist and the database connection is showing as 'blerg' not the 'blerg-static-api' which it should be.

任何帮助将非常感谢。

Any help would be much appreciated thanks.

推荐答案

尝试 establish_connection / code>。

Try to establish_connection in ContentItem too.

这篇关于无法连接到rails 3.2中的两个postgres数据库。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆