6

Railsでの複数接続にactive_delegateを使用しています。ここでは、一部のモデルでは master_database として mysql を使用し、他のモデルでは 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/minhghivn/my_replication

于 2011-09-21T07:04:44.490 に答える
1

また、2 つの異なるデータベースに接続して管理する必要があったため、secondbase という gem を作成しました: http://github.com/karledurante/secondbase

于 2010-12-07T15:55:43.060 に答える
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  

私は開発モードのみで雑種を起動します。

ここに私のモデルのスーパークラスがあります

$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 に答える
0

active_delegate については知りませんが、最近、仕事用のアプリケーション用に別のデータベースにアクセスする必要がありました。だから私は自分自身のために何かを書きました。私たちが話しているように、それは本番アプリケーションで実行されています。

固定リンクconnection_ninja

于 2009-08-19T10:55:40.553 に答える