ActiveRecord Without Rails

Some of you must be wondering, I must be insane to use ActiveRecord outside of Rails. But there are some very compelling reasons.

  • ActiveRecord is an awesome library for Object Relational Mapping (ORM).
  • Other projects might not involve the use of Rails.
  • You want to experiment with ActiveRecord prior to creating a plugin for your Rails project.
  • A library for ORM is already written (why reinvent the wheel?)
  • Your just very bored.

Whatever the case may be, it’s best to be prepared.

Installing ActiveRecord

If you already have Rails installed via RubyGems, then your already set to go. Else install it via:

sudo gem install activerecord

Playing with ActiveRecord

Open up a file, and insert the following:

Now if you are familiar with Rails, this is the same configuration as seen in your database.yml file ( RAILS_ROOT/config/database.yml File). There are more options for how to connect to a database, just check the ActiveRecord::Base.establish_connection documentation.

Moving Database Configuration to Another File

Note: It’s not safe to save your database configuration in the same file as you code. In case you distribute your code, and forget to remove your password. That wouldn’t be a cool scenario. (Well it would also be pointless if you sent your database configuration file as well).

So lets make a database.yml and dump the information in there.

adapter: mysql
database:  database
username: user
password: password
host:      localhost

Now our previous file that utilizes ActiveRecord will look like such:

Playing with ActiveRecord Safely!

And now your set to playing around with ActiveRecord without Rails!

Debugging ActiveRecord SQL Queries

Sometimes you run into problems, and suspect your SQL isn’t generated as it’s supposed to be. But how do we debug this?

This will output it to standard error (in which most cases its) your screen. Sadly this can also clutter your screen.

Debugging ActiveRecord SQL Queries to a File

Now if you know your shell well, you can pipe it to a file via: ruby connect.rb 2>> database.log Terminal. But you may forget to pipe it, or what not.

So replace the line containing ActiveRecord::Base.logger with

Warning: I chose to append (’a') the output to the file. This may result in very large files. You can chose to use overwrite (’w') instead.

From here on you can watch the file with: tail -f database.log Terminal.

Funky/Colorized Logs

Note: If you view the database.log file, you’ll notice the following. This is because ActiveRecord colorizes output for viewing in a terminal. But when viewed as a normal file, you get garbage.

  ^[[4;36;1mSQL (0.000273)^[[0m   ^[[0;1mSELECT count(*) AS count_all FROM users ^[[0m

To disable colorize logging, enter:

Extra Resources

11 Comments »

  1. Chris Dwan said,

    May 25, 2007 @ 1:46 am

    Nice work, this is a perfect resource for getting the picture on how to use AR on it’s own! Thanks for posting this.

  2. Matt King said,

    May 25, 2007 @ 1:55 am

    This is awesome, thanks! I can’t believe it was so easy to include ActiveRecord into a small Ruby project I was working on. Getting all of the ORM stuff for free made things go way faster.

  3. aizatto said,

    May 25, 2007 @ 9:32 am

    Your welcome :), if there are any other things you’d like to see, do tell!

  4. Frank said,

    January 24, 2008 @ 11:29 pm

    Thanks so much. I have been looking for a way to get a log of the actual SQL ActiveRecords generates for some time and couldn’t find it. This is great!!!

  5. Frank said,

    January 25, 2008 @ 11:47 pm

    aizatto , I was wondering if you could tell us a bit more about how to set up the envirnoment for using AcviteRecords without Rails. I guess it would be best to have a directory structure very similar to what Rails generates. If I do that and I set up a seperate file for each model in the model directory - would I end up having to ‘require ‘ each individual file in my controllers? Rails does some magic there and I don’t know how it works. Thanks for your help!

  6. :: aina :: said,

    March 4, 2008 @ 3:51 pm

    i’m newbie :)
    aizatto, where should i put this base connection file?
    thanks for this tutorial as well :D

  7. ernst said,

    April 21, 2008 @ 6:51 pm

    thanks soooo much

    makes it trivial to write a tiny ruby script that can use the power of active record!

  8. Vidul said,

    May 10, 2008 @ 6:34 pm

    Nice work.
    One small note:
    YAML::load(File.open(’database.yml’))
    can be shorten and clearer:
    YAML::load_file(’database.yml’)

  9. dubek said,

    July 6, 2008 @ 6:49 pm

    To disable coloring, you should run the setter method on ActiveRecord::Base (and not on ActiveRecord, which is a Module). So the last line should be changed to:


    ActiveRecord::Base.colorize_logging = false

  10. rowtheboat - George Palmer » ActiveRecord outside of Rails (and YAML) said,

    July 17, 2008 @ 3:14 am

    […] but I wanted to follow best practices and keep my code DRY. After a quick search I found a clean solution that allows you to re-use the database.yml configuration file from the rails […]

  11. Aparna said,

    August 26, 2008 @ 3:13 am

    The ‘without rails’ feature of your posts has been most useful in getting me to implement hello world programs :) as a course of my learning to program in ruby

RSS feed for comments on this post · TrackBack URI

Leave a Comment