# Re: ActiveRecord without Rails

**URL:** <https://www.ruby-forum.com/t/re-activerecord-without-rails/48478>\
**Category:** Ruby\
**Created:** [November 21, 2005, 5:02am UTC](https://www.ruby-forum.com/t/re-activerecord-without-rails/48478 "2005-11-21T05:02:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@rdm](https://www.ruby-forum.com/u/rdm)\
**Post date:** [November 21, 2005, 5:02am UTC](https://www.ruby-forum.com/t/re-activerecord-without-rails/48478/1 "2005-11-21T05:02:07Z")

</div>

This may help…

— Begin Forward —

From: Deirdre Saoirse M. [removed\_email\_address@domain.invalid](mailto: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`)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

…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”  
end

# Read database config via YAML

@dbs = YAML::load(ERB.new(IO.read(“…/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

database

# assumption: data file is one level up from rails app

# assumption 2: data is tab-delimited with a return between lines.

f = File.open(“…/…/users\_export.tab”, “r”)  
f.each\_line(“\r”) do |line|  
u = ImportUser.new  
u.id, u.username, u.first\_name, u.last\_name, u.password, u.email =  
line.chomp.split(“\t”)  
u.save!  
end  
f.close

–  
\_Deirdre

> **[Deirdre Saoirse Moen – Deirdre Saoirse Moen](https://deirdre.net/)**
>
> Sounds Like Weird

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

— End Forward —
