Home arrow Ruby-on-Rails arrow Page 2 - Ruby-on-Rails: Understanding the Basics of Active Record
RUBY-ON-RAILS

Ruby-on-Rails: Understanding the Basics of Active Record


Ruby on Rails handles data manipulation with the Active Record, which provides an Object Relational Mapping framework. If these sound a little like foreign words to you, keep reading; you'll find that the way RoR handles ORM is a little different, and perhaps easier, than you might see it handled with other languages.

Author Info:
By: APRajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 24
October 11, 2006
TABLE OF CONTENTS:
  1. · Ruby-on-Rails: Understanding the Basics of Active Record
  2. · Active Record: What is it?
  3. · Active Record: Steps to using it
  4. · Rails in the Real World

print this article
SEARCH DEVARTICLES

Ruby-on-Rails: Understanding the Basics of Active Record - Active Record: What is it?
(Page 2 of 4 )

Active Record is an ORM framework or layer distributed along with RoR. Even though the term distributed is used, the Active Record is built into the core of RoR. Since Active Record is an ORM layer, it provides the following mapping services:

1. Tables to Classes

2. Columns to Attributes

3. Primary Keys to Ids

4. Rows to Objects

The major difference between Active Record and other ORM frameworks/layers is the way mapping is done. Most of the popular ORM frameworks such as Hibernate use XML as the mapping container. However Active Record uses a convention-over-configuration methodology. Let me show you how it is done.

Tables to Classes

To map a table to a class, the class has to be derived from ActiveRecord::Base i.e. the Base class present within the ActiveRecord package. What essentially happens when  a class is derived from ActiveRecord::Base is that the derived class acts as a wrapper for the database table. In understanding the tableís name, Active Record assumes that the pluralized name of the class is the name of the table. If the class name has multiple capital letters, it is assumed that the table name contains underscores between the words. Some of the examples are:

Table Name

Class Name

Orders

Order

Line_Items

LineItem

Data

Datum

    This behavior can be turned off by the following two steps:

  • Set the global flag ActiveRecord::Base.pluralize_table_names to false. This flag is present in the environment.rb file in the config directory.
  • Override the default generation of a table name using the set_table_name directive.

For example, if a table named Orders is to be mapped to the class Order the code would be:

class Order < ActiveRecord::Base end

But if the class Order needs to be mapped to a table Order_QA then the code would be:

class Order <ActiveRecord::Base set_table_name "Order_QA" # Not "Orders" end

Now let's see how columns are mapped to attributes.

Columns to Attributes

Once a table has been mapped to a class, there is no requirement to explicitly map the columns to the attributes. Thatís because Active Record determines the attributes of a table dynamically at runtime. Typically, Active Record reflects on the schema to configure the class that wraps the table. The following shows how the SQL data types are mapped to Rubyís data types:

Next let's look at how Primary key is mapped.

Primary Key to Ids

If you remember the example application from the first part of this series, the tableís Primary key was named id and had the data type of integer. Though the logical one would have been the Order_id, yet this id was used. At first glance, this might be considered preposterous. However, if one looks at the long run, the id as the Primary Key has its benefits.

Let's say that the Order_Id is based on a 16 digit format including the Item id, user id, and so forth. In the future if the Order_Id has to be increased to 20 digits by including the last 4 digits of an RFID based code, all the dependent tableís columns would have to be changed. Thatís a large order for even a medium-sized application. The mapping is done dynamically. However, if the requirement is to map an existing  Primary Key then it can be done as follows:

class Order <ActiveRecord::Base set_primary_key "orderId" end

Rows to Objects

Whenever a retrieve operation is performed on a class, the corresponding SQL query is fired at the database and the row is retrieved, which is then used to populate the object of the same class. The values of the column in the retrieved row become the values of attributes of the object. In a nutshell, the row is mapped into the object. For example, the following returns an Order object having the id of 27:

 

an_order = Order.find(27)

 

That covers mapping. Now let's see the steps involved in performing CRUD operations using Active Records.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials