Using YAML to Manage Database Configurations

Here’s a simple enhancement to my previous entry regarding using ActiveRecord – we’re using YAML to move the database connection information outside of the Ruby file.  The “connect” method is changed as follows:

require 'active_record'
require 'yaml'

class ActiveRecordTest
  # Create a connection.
  def connect
    #use YAML to define the connection information.
    dbconfig = YAML::load(File.open('database.yml'))
    ActiveRecord::Base.establish_connection(dbconfig)
  end
...

and the YAML file looks like this:

adapter: postgresql
database: Test
username: [username]
password: [password]
host: localhost

Again replacing [username] and [password] with the actual username and password that has the desired permissions on your database.

Advertisements

One thought on “Using YAML to Manage Database Configurations

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s