Module: MultiConfig::ORMs::ActiveRecord::ClassMethods

Defined in:
lib/multi_config/orms/active_record.rb

Overview

This ‘acts_as` style extension provides the capabilities for using multiple database configuration files. Any model can specify what database config file to use by using a Rails 3.2 style self.config_file= method.

Example:

class DifferentTable < ActiveRecord::Base
  self.config_file = 'other_db'
end

In config/other_db.yml

development: &development
  database: db/other_db
  host: localhost
  adapter: sqlite3

test:
  <<: *development

Instance Method Summary collapse

Instance Method Details

#config_file=(file_name) ⇒ Object

Use the specified config file for database. You can specify file without .yml extension. If you specify database.yml or database, it will not have any effect since that is loaded by default.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/multi_config/orms/active_record.rb', line 36

def config_file=(file_name)
  file_name += '.yml' unless File.extname(file_name).eql? '.yml'
  # Load config file if it is not database.yml
  unless file_name == 'database.yml'
    namespace = File.basename(file_name, '.yml')
    # Update active_record configurations hash
    add_db_config(file_name, namespace)
    # Raise error if the config file does not have the current environment
    raise "Configuration for #{::Rails.env} environment not defined in #{Config.path file_name}" unless configurations.include? "#{namespace}_#{::Rails.env}"
    # Establish connection. This is the only way I found to different database config. Will try to find alternative
    establish_connection :"#{namespace}_#{::Rails.env}"
  end
end