Active Record in Rails

by admin on December 15, 2008

Any web developer will know how important a database and its relationship can be to their web application. With that being said i would like to introduce you to Active Record.  Active Record is a Ruby library that allows your Ruby programs to transmit data and command to and from various data stores, which are usually relational databases.  You can also say that Active Record allows Ruby to work with databases.

Brief History behind Active Record

Active Record is a design pattern originally published by Martin Fowler in his book Patterns of Enterprise Application Architecture.  David Heinemeier Hansson (the creator of the Rails framework) took the concepts laid out by Mr. Fowler and implemented them as a Ruby library called Active Record.

Active record has been released with the Rails framework to the public and is also available as a part of the core bundle with its own Ruby Gem.

Active Record and the ORM Pattern

At the core of Active Record is object relational mapping or “ORM.”  These relational databases can be represented well by object based code; if we look at an example of a dummy accounts table in a typical database it would look like this:

Accounts table
     ID field (integer; auto-incremented; primary key)
     Username field (text type field)
     Password field (text type field)

If we look at the Active Record Account class (or model) it would look something like this:

Class Account ActiveRecord::Base
#some code here
end

If we look through our Rails code, we could possible create instances of different account objects like below:

# creates a new account object in memory and a new account record in our database
newacc = Account.new
newacc.Username = "Ahad"
newacc.Password = "pass*$#"
newacc.save
# creates an Account object in memory from data in Account table with ID of 1
# (equivalent to the ANSI SQL statement of "select * from accounts where ID = 1")
findacc = Account.find(1)
# deletes records from database that have username of "Ahad"
Account.delete("username = 'Ahad'")

Active Record Differs from other ORM Libraries

Active record out of the box makes a number of configuration assumptions, without requiring any outside configuration files or mapping details.  The previous example takes advantage of Active Record Assumptions, so we are not required to to configure or set up any specific instructions.  This is unlike many ORM libraries such as Java’s Hibernate.

MVC Concepts and Active Record

Active Record is most famous as being an important part of the Ruby on Rails framework, and is being copied by many other frameworks such as Code Igniter.    What MVC does it that it breaks code into logical groupings and programs into logical functional groupings.  With Rails, the model section is generally your Active Record classes and other data-descriptive or datacommunication code. The view section remains primarily for the user interface, which tends to be a heavy dose of HTML in most Rails applications.

CRUD Database Transactions and Active Record

Ususally there are four tasks one can perform when working with databases, and as a group are referred ot as CRUD. 

  1. (C): Creating 
  2. (R): Retrieving
  3. (U): Updating
  4. (D): Deleting
newacc = Account.new(:username = "Ahad")
newacc.save # creates the new record in the account table
temp = Account.find(1)
# => selects the record associated with id of 1 from the account table
temp.username = 'Ahad' # => assigns a value to the username attribute of the object
temp.save # does the actual update statement applying the changes we just stated.
Account.destroy_all(1) # deletes the record in the account table with id of 1

There are many more options to that we can consider here, but the above is the most generic or the most common.

How the Active Record Library applies to Ruby code

What you have to remember is when working with Active Record is that its all Ruby code.  Anything you do with Ruby objects (inheritance, overriding methods, etc etc) can also be done with Active Record objects. The whole idea is to represent database records and objects, but they really are two separate things; Ruby onjects and database records.

Active Record:  The smart choice

There are many reasons why Active Record is the smart choice.  One is its easy to install, simple to write and read, and also a full feature object based code.  If you look below at the benefits of Acitve Record, there is no doubt that this library is worth learning about.

  • Simplified configuration and default assumptions (Convention over Configuration).
  • Associations among objects.
  • Automated mapping b/w tables and classes and b/w columns and attributes.
  • Data Validations.
  • Callbacks
  • Inheritance hierarchies.
  • Direct manipulation of data as well as schema objects.
  • Database abstraction through adapters.
  • Logging support.
  • Migration support.
  • Active Record integrated in other emerging frameworks like Merb.

{ 1 comment… read it below or add one }

Jan SeidlNo Gravatar December 27, 2008 at 8:34 am

Yeah, the ActiveRecord (such as many paradigms collected by Heinemeier on Rails) is broadly used around the planet.

I don’t thing I would say other FW copied. This statement is very commom since Rails first introduced but someone introduced to the world after Rails used it. There are goods paradigms envolved in the Rails architechture that sould be used in other projects.

You know, if it’s good, it should be used! (Windows is not good! Don’t use! haha)

Regards

Reply

Leave a Comment