6

我在 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

有人有什么解决办法吗??

4

7 回答 7

11

另一种方式:

class Abc < ActiveRecord::Base
  establish_connection Rails.configuration.database_configuration["test"]
end
于 2010-12-10T04:20:54.237 回答
5

现在可能想查看https://github.com/tchandy/octopus

于 2010-11-03T05:17:21.997 回答
4

这将更改单个模型对象的数据库连接。

$config = YAML.load_file(File.join(File.dirname(__FILE__),
   '../config/database.yml'))

class ModelWithDifferentConnection < ActiveRecord::Base
  establish_connection $config['connection_name_from_database_yml']
end

如果您使用的是同一台服务器,但只是使用不同的数据库文件,那么您可以执行类似的操作。

class ModelWithDifferentConnection < ActiveRecord::Base

  # Lives in the CURRICULUM database
  def self.table_name
    "database.table"
  end

end
于 2009-08-19T13:00:57.280 回答
2

我强烈建议MySQL 适配器的MyReplication插件,它可以帮助您在运行时以优雅的方式切换连接:

User.using(:another_database) do
  u = User.all
end

https://github.com/minhnghivn/my_replication

于 2011-09-21T07:04:44.490 回答
1

我试过你的样品,还是出错!!

superclass mismatch for class MysqlAdapter

我想,问题出在我的database.yml文件上。请检查这个文件

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

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

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

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

我只在developemnet 模式下启动mongrel。

这是我的模型超类

$config = YAML.load_file(File.join(File.dirname(__FILE__),
   '../../config/database.yml'))

class MasterDatabase < ActiveRecord::Base
    self.abstract_class = true
    establish_connection $config['database_mysql']    
end 

请纠正我..

于 2009-08-20T07:12:52.993 回答
1

我还必须连接和管理两个不同的数据库,所以我创建了一个名为 secondbase 的 gem:http: //github.com/karledurante/secondbase

于 2010-12-07T15:55:43.060 回答
0

我不知道 active_delegate,但我最近不得不访问不同的数据库以获取工作应用程序,并且没有什么真正适合我想要的。所以我为自己写了一些东西,正如我们所说的,它正在生产应用程序中运行。

固定链接connection_ninja

于 2009-08-19T10:55:40.553 回答