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.
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:
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
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"
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
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.