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('database.yml'))

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.


One thought on “Using YAML to Manage Database Configurations

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s