Forum: Ruby Re: ActiveRecord without Rails

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
rdm (Guest)
on 2005-11-21 06:02
(Received via mailing list)
This may help...

  --- Begin Forward ---

  From: Deirdre Saoirse M. <removed_email_address@domain.invalid>
  Subject: Re: [Rails] ActiveRecord without Rails
  Date: Tue, 8 Nov 2005 14:24:34 -0800
  On Nov 7, 2005, at 11:58 AM, CLung wrote:

  > Anyone know of a good site that has info/examples of using
  > ActiveRecord without Rails?  I've done a few Googles but cannot
  > seem to get a whole lot back.

  Here's a working entire example from a post to a Ruby list.

  I've been needing to do some ActiveRecord work outside of rails
  proper lately, mostly to do things like import a bunch of existing
  records from another database.

  I wrote a variant of this for BayCon (who exported from Filemaker,
  thus the return rather than newline between records), then wound up
  needing it on another project too.

  So, here's a short example. Note that I am using my rails db
  connection and I keep this script somewhere in my Rails tree (/lib
  or /db, depending), so that's why I'm not passing a YAML file for the
  connection configuration.

  [excluding bit about database.yml because this is a rails list]

  ...and here's the db table we're looking at inserting into:

  CREATE TABLE `users` (
      `id` int(11) NOT NULL auto_increment,
      `username` varchar(50) NOT NULL,
      `first_name` varchar(50) NOT NULL,
      `last_name` varchar(50) NOT NULL,
      `password` varchar(80) NOT NULL,
      `email` varchar(75),
      `created_at` timestamp,
      PRIMARY KEY  (`id`)

  ...and here's the script as a whole.

  #!/usr/bin/env ruby

  require 'rubygems'
  require_gem 'activerecord'

  # if I used the User class in the rails app, it'd have to follow all
  the current
  # validations rather than the non-existent ones in actual existing
  data. Thus,
  # create a little class without that problem.
  class ImportUser < ActiveRecord::Base
     set_table_name "users"

  # Read database config via YAML
  @dbs = YAML::load("../config/database.yml")).result)

  # to slurp records into production db, change this line to production.
  curr_db = @dbs["development"]

  # these parameters are mysql-specific, figure out how to improve
  ActiveRecord::Base.establish_connection(:adapter => curr_db
  ["adapter"], :database => curr_db["database"],
     :host => curr_db["host"], :username => curr_db
  ["username"], :password => curr_db["password"])

  # read each line from file, split into values, and insert into
  # assumption: data file is one level up from rails app
  # assumption 2: data is tab-delimited with a return between lines.

  f ="../../", "r")
  f.each_line("\r") do |line|
     u =, u.username, u.first_name, u.last_name, u.password, =


  Rails mailing list

  --- End Forward ---
This topic is locked and can not be replied to.