Multiple database connection in Rails

前端 未结 7 1232
说谎
说谎 2020-12-06 07:53

I\'m using active_delegate for multiple connection in Rails. Here I\'m using mysql as master_database for some models,and postgresql for some other models.

Problem i

相关标签:
7条回答
  • 2020-12-06 08:08

    might want to check out https://github.com/tchandy/octopus nowadays.

    0 讨论(0)
  • 2020-12-06 08:09

    Another way:

    class Abc < ActiveRecord::Base
      establish_connection Rails.configuration.database_configuration["test"]
    end
    
    0 讨论(0)
  • 2020-12-06 08:16

    I don't know about active_delegate, but I recently had to access different databases for work applications, and nothing really fit what I wanted. So I wrote something for myself, it's running in production applications as we speak.

    Fixed Link connection_ninja

    0 讨论(0)
  • 2020-12-06 08:22

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

    0 讨论(0)
  • 2020-12-06 08:24

    I highly suggest MyReplication plugin for MySQL adapter which helps you switch the connection at run-time in an elegant way:

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

    https://github.com/minhnghivn/my_replication

    0 讨论(0)
  • 2020-12-06 08:27

    I tried ur Sample,still getting error!!

    superclass mismatch for class MysqlAdapter
    

    I think ,the problem is with 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 the mongrel in developemnet mode only.

    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..

    0 讨论(0)
提交回复
热议问题