Connection to Mysql

When I need to do this, I create a script that loads up the rails environment so I have access to all the application stuff, and then use a manual database connection. Let's say we're importing customers from an old database:

------- /scripts/import_from_old_db.rb #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/environment' require 'mysql'

dbh = Mysql.real_connect("mydatabase", "user", "pass", "myolddatabase") old_customers = dbh.query("select * from tblCustomers")

customers = while old_customer = old_customers.fetch_hash do     customers << {:name => customer["Name"], :address => customer["Add"]} end

Customer.create(customers) dbh.close