Using ActiveRecord for Simple Maintenance Scripting

Posted by Corban Brook Tue, 09 Aug 2005 20:12:00 GMT

Over the last year the new ruby web framework, Ruby on Rails, has been rapidly maturing and a 1.0 release is due to push before RubyConf2005 in October. Rails is designed for RAD utilizing code generators and scaffolding allowing developers to quickly prototype interfaces to database tables . A great deal of the Rails magic comes from the ActiveRecord library which implements ORM to effectively map database tables to classes and appends CRUD functionality.

The purpose of this article is to show how to use the ActiveRecord library standalone in your own scripts.

Installation

This article assumes you already have Ruby and RubyGems installed.

Prerequisites

Installing ActiveRecord

Luckily, ActiveRecord is available as a rubygem package (think of it as ports for ruby). Installing the gem is quick and easy.

First, Execute the following command: gem install activerecord

The Rubygem program will start and automatically begin looking for the ActiveRecord gem, first on the local machine and then on the remote gem repository.

ActiveRecord has a dependency with the ActiveSupport Library. You will be prompted to install the ActiveSupport gem during the install process.

Attempting local installation of 'activerecord'
Local gem file not found: activerecord*.gem
Attempting remote installation of 'activerecord'
Install required dependency activesupport? [Yn] y
Successfully installed activerecord-1.11.1
Successfully installed activesupport-1.1.1
Installing RDoc documentation for activerecord-1.11.1...
Installing RDoc documentation for activesupport-1.1.1...

Ensure that the gems were installed properly by using the gem list command. You should see the gems listed now.

activerecord (1.11.1)
    Implements the ActiveRecord pattern for ORM.

activesupport (1.1.1)
    Support and utility classes used by the Rails framework.

Using ActiveRecord

Connecting to a Database

The following block of code includes RubyGems and the ActiveRecord Gem then establishes a connection to the database.

ActiveRecord includes adapters for many databases including: MySQL, PostgreSQL, SQLite, SQL Server, DB2, and Oracle (Maybe more by the time you read this article).

require "rubygems" 
require_gem "activerecord" 

ActiveRecord::Base.establish_connection(
  :adapter  => "mysql",
  :host     => "your.db.host",
  :database => "phonebook",
  :username => "admin",
  :password => "secret" 
)

Note: You will need to specify the Unix socket if you are connecting to localhost, eg. :socket => "/var/run/mysqld/mysqld.sock". If you which to connect to the TCP socket instead, simply specify 127.0.0.1 as the host.

Models

Models are classes which represent tables in the database. In this example we are assuming we have 2 tables: person, and person_phone. The person table contains fields relating to personal information and the person_phone contains a phone information (number, work/home/cellular, etc.) and a foreign key to the person table.

Although ActiveRecord expects a certain database schema you can manualy set the table and primary key names if your schema differs from the Rails nomenclature.

The Object Relational Mapping features really come into play at this stage. In order for the classes to relate back and forth which each other relationships between the tables must also be specified. A one-to-many relationship between the Person and PersonPhone classes is established with the has_many :person_phone method and the reverse relationship in the PersonPhone class with the belongs_to :person method.

class Person < ActiveRecord::Base
  set_table_name 'person'
  set_primary_key 'person_id'
  has_many :person_phone
end

class PersonPhone < ActiveRecord::Base
  set_table_name 'person_phone'
  set_primary_key 'person_phone_id'
  belongs_to :person
end

Making Shit Happen

Now instantiate the object and you are ready to use all of active records functionality with your database.

person = Person.new
person.name = 'John Smith'
person.person_phone.number = '555-4567'
person.save

Thats all folks.

Posted in ,  | no comments | no trackbacks

Trackbacks: 0

Use the following link to trackback from your own site: http://schf.uc.org/articles/trackback/1

Comments: 0

Leave a response | RSS feed for this post

Leave a response

Toggle website and email fields