mysql - Rails 中的多个数据库连接

标签 mysql ruby-on-rails postgresql multiple-databases

我在 Rails 中使用 active_delegate 进行多重连接。在这里,我使用 mysql 作为某些模型的 master_database,并使用 postgresql 作为其他一些模型。

问题是,当我尝试访问 mysql 模型时,出现以下错误!堆栈跟踪显示,它仍在使用 postgresql 适配器访问我的 mysql 模型!

RuntimeError: ERROR C42P01  Mrelation "categories" does not exist   P15 F.\src\backend\parser\parse_relation.c  L886    RparserOpenTable: SELECT * FROM "categories" 

STACKTRACE
===========
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:507:in `execute'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:985:in `select_raw'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/postgresql_adapter.rb:972:in `select'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in `select_all'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:81:in `cache_sql'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:60:in `select_all'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:661:in `find_by_sql'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1553:in `find_every'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:615:in `find'
D:/ROR/Aptana/dedomenon/app/models/category.rb:50:in `get_all_with_exclusive_scope'
D:/ROR/Aptana/dedomenon/app/models/category.rb:50:in `get_all_with_exclusive_scope'
D:/ROR/Aptana/dedomenon/app/controllers/categories_controller.rb:48:in `index'

这是我的database.yml 文件

postgre: &postgre
  adapter: postgresql
  database: codex
  host: localhost
  username: postgres
  password: root
  port: 5432  

mysql: &mysql
  adapter: mysql
  database: project
  host: localhost
  username: root
  password: root
  port: 3306  

development:
  <<: *postgre

test:
  <<: *postgre

production:
  <<: *postgre

master_database:
  <<: *mysql

我的master_database模型是这样的

class Category < ActiveRecord::Base

  delegates_connection_to :master_database, :on => [:create, :save, :destroy]

end

谁有解决办法??

最佳答案

另一种方式:

class Abc < ActiveRecord::Base
  establish_connection Rails.configuration.database_configuration["test"]
end

关于mysql - Rails 中的多个数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1298909/

相关文章:

postgresql - PL/pgSQL : Can we store a query result into a variable

ruby-on-rails - 当 default_url_options trailing_slash 为 true 时,Rails root_url 有两个正斜杠

javascript - 在 OPENSIPS 中使用哈希表加速 Mysql 查询

mysql - 我如何优化这个子查询?

php - 我获得独特页面浏览量的方法有效吗?

ruby-on-rails - 如何访问数组中的哈希值?

mysql - 使用原始sql在迁移中添加复合主键

postgresql - 从 2 个查询中减去结果

sql - 外键列可以为NULL的查询

php - 点击获取某个单元格的表格数据