Multiple database connection in Rails

前端 未结 7 1233
说谎
说谎 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:30

    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 are using the same server but just a different database file then you can do something like this instead.

    class ModelWithDifferentConnection < ActiveRecord::Base
    
      # Lives in the CURRICULUM database
      def self.table_name
        "database.table"
      end
    
    end
    
    0 讨论(0)
提交回复
热议问题