6.1. Plugging In to Your Legacy

For the purposes of having something concrete to wrap your head around, let's assume that Soups OnLine has secured a strategic coup: the ability to deliver information from Ingredientopedia, the world's leading online source of information about the kind of ingredients that one might use in, say, a soup. Sadly, due to the sort of contrivance that involves armies of fictional lawyers, Soups OnLine has to use the existing database as-is, and is not free to migrate the schema to something useful. So you're stuck with a database schema that looks, in part, like this (a naming convention only a database admin could love):

CREATE TABLE 'ingredientopedia'.'the_ingredient_table' (
  'ingredient_id' INTEGER AUTO_INCREMENT,
  'ingredient_name' VARCHAR(255),
  'ingredient_description' VARCHAR(4000),
  'ingredient_nutritional_info' VARCHAR(4000),
  'ingredient_category_id' INTEGER,
  PRIMARY KEY ('ingredient_id')
)
CHARACTER SET utf8;

To get Rails to open this database, you need to adjust the database.yml file to add three more database environments: one for each Rails environment. You can use the YAML merge feature to make the declaration more compact, like this:

ingredient: &ingredient
  adapter: mysql
  username: root
  password:
  socket: /tmp/mysql.sock
ingredient_development:
  database: ingredientopedia_development
  <<: *ingredient

ingredient_production:
  database: ingredientopedia_production
  <<: *ingredient

ingredient_test:
  database: ingredientopedia_test
  <<: *ingredient

Get Professional Ruby on Rails™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.