This may help... --- Begin Forward --- From: Deirdre Saoirse Moen <deirdre / deirdre.net> 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 http://deirdre.net ______________________________________________ Rails mailing list Rails / lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails --- End Forward --- -- email: rdm / cfcl.com; phone: +1 650-873-7841 http://www.cfcl.com - Canta Forda Computer Laboratory http://www.cfcl.com/Meta - The FreeBSD Browser, Meta Project, etc.