ActiveRecord: Ruby on Rails Optional

Ruby on Rail is made up of a variety of code modules packaged as Ruby Gems. For example, Rails is composed of gems such as ActionController, ActionView, ActionRecord, and few other gems. It might sound surprising at first, but you can use ActiveRecord without the other Rails components. ActiveRecord is an Object Relational Mapper (ORM), it binds database records to Ruby classes that act as models to your data. ActiveRecord models allow you to post and query data to and from a relational database.

To use ActiveRecord, you will need to have it installed in your system. If you already have Rails, ActiveRecord is already part of your Ruby gems environment. If not, you can install it via the following command.

sudo gem install activerecord

Once the ActiveRecord gem is installed in your system, you can require it in your Ruby scripts.

require 'rubygems'
require 'active_record'

In a Ruby on Rails project, the only configuration not defaulted through convention is the information required to setup your database connection. In Rails, you usually have a config/database.yml file to point to the right database used in your web application. This information is passed to ActiveRecord to establish a database connection. In a Ruby script, a non-Rails project, you can manually create a database connection with the following snippet.

  :adapter => 'mysql',
  :database => DB_NAME,
  :username => DB_USERNAME,
  :password => DB_PASSWORD,
  :host => LOCALHOST,
  :port => DB_PORT

Obviously, you would need to replace the placeholders for the correct database name, username, and password to connect to your database.

There are times when you might need to enable the logger. You might get an undefined method or nil object error if you don’t set a value to the ActiveRecord logger. You can configure ActiveRecord to log to the standard error or standard output. To accomplish this, first require the logger gem then initialize and set the ActiveRecord logger.

ActiveRecord::Base.logger =

That is essentially all that is required. Once you have a database connection, you can create models as you would in a Rails project.

class Post < ActiveRecord::Base

p = => title, :body => body)

If you require ActiveRecord in your Ruby script, you can also use it to create database schema migrations. Once you have established an connection to the database you can run a schema migration such as the following.

create_table "users", :force => true do |t|
  t.integer "twitter_id", :default => 0, :null => false
  t.string "user_name", :limit => 20, :default => "", :null => false
  t.string "screen_name", :limit => 30, :default => "", :null => false
  t.string "location", :limit => 50, :default => "", :null => false
  t.string "url", :limit => 50, :default => "", :null => false

There are a few other ORMs written in Ruby, such as DataMapper and Sequel. One key factor of using ActiveRecord is that if you had dabbled with Rails before you already have it in your system, you don’t have to learn anything new, and you can be productive with it rather quickly.

Related posts:

  1. High Rolling With Rails
  2. Acts As Voteable Rails Plugin
  3. Riff Rails Plugin
  4. Rad Ruby On Rails
  5. Acts As Rateable Plugin
  6. Acts As Commentable Plugin

One Response to “ActiveRecord: Ruby on Rails Optional”

  • Julien Says:

    That is promising… however, I have an issue with your database conection. Let’s say I create an app that runs in the back. Is it a big deal to have the MySQL connection kept alive “forever”? What happens if it is deconnected?

    By the way, how does it work in Rails? one connection per request?

Leave a Reply