Multiple Database Connections in Rails

I use active_delegate for multiple connections in Rails. Here I use mysql as master_database for some models, and postgresql for some other models.

The problem is that when I try to access mysql models, I get the error below! The stack trace shows that it is still using the postgresql adapter to access my mysql models!

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' 

here is my database.yml file

 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 

and my master_database model is master_database this

 class Category < ActiveRecord::Base delegates_connection_to :master_database, :on => [:create, :save, :destroy] end 

Anyone have any solution?

+6
mysql ruby-on-rails postgresql
source share
7 answers

Another way:

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

you may need to check out https://github.com/tchandy/octopus currently.

+5
source share

This will change the database connection for a single model object.

 $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 

If you use the same server, but only a different database file, you can do something like this.

 class ModelWithDifferentConnection < ActiveRecord::Base # Lives in the CURRICULUM database def self.table_name "database.table" end end 
+4
source share

I highly recommend the MyReplication plugin for the MySQL adapter, which helps you switch the connection at runtime in an elegant way:

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

https://github.com/minhnghivn/my_replication

+2
source share

I tried ur Sample, still getting the error!

 superclass mismatch for class MysqlAdapter 

I think the problem is in my database.yml file. Please check this file.

 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 

I start mongrel only in developemnet mode.

here is my model superclass

 $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 

Please correct me ..

+1
source share

I also had to connect to and manage two different databases, so I created a gem called secondbase: http://github.com/karledurante/secondbase

+1
source share

I do not know about active_delegate, but I recently had to access various databases for working applications, and nothing matched what I wanted. So I wrote something for myself, it works in production applications when we talk.

Fixed link connection_ninja

0
source share

All Articles