Skip to content

Instantly share code, notes, and snippets.

@shannonwells
Created October 8, 2014 23:33
Show Gist options
  • Save shannonwells/a068697f15bc4b4062f9 to your computer and use it in GitHub Desktop.
Save shannonwells/a068697f15bc4b4062f9 to your computer and use it in GitHub Desktop.
How to set up using a different database in rails from the default
class MyclassBase < ActiveRecord::Base
self.abstract_class = true
CONFIG_PATH = File.join(Rails.root, 'config', 'database_myclass.yml') unless defined? CONFIG_PATH
# So you can use environment variables in your YML file.
erb = ERB.new(File.read(CONFIG_PATH)).result
config = ActiveSupport::HashWithIndifferentAccess.new(YAML.load(erb))[Rails.env]
raise ConfigurationNotFound.new("Configuration not found for config_path: '#{CONFIG_PATH}'") unless config
establish_connection(
adapter: config[:adapter],
username: config[:username],
password: config[:password],
dataserver: config[:dataserver],
database: config[:database]
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment