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.
ActiveRecord::Base.establish_connection(
: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 = Logger.new(STDERR)
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
end
p = Post.new(:title => title, :body => body)
p.save
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
end
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.