Monday, October 10, 2011

A glimpse of Active Record



What is Active Record ....

Active Record controls the interaction between your application and the database. The main advantage of Active Rec
ord is, it reduces your configuration.Also Active Record's meta-programming dynamically adds capabilities to your classes based on the contents and structure of database. Finally it reduces the coding effort from the developer.


How Active Record works ...

With any Active Record implementation , users manipulate database tables through record objects. Each Active record object has CRUD (Create, Read, Update, Delete) methods for database access. This kind of mechanism allows a simple design and direct mapping between database and the application.

Most of the frameworks follow the strategy called ORM (Object Relational Mapping). In this technique, database tables and objects are created independently. Then a mapping created between objects and database tables using some code. The drawback for this mapping technique is , you need to specify each colun in database, in object models and in configuration file (sometime).

Instead of using mapping technique , Active Record uses wrapping technique. After building the relational database, user wraps each table with Active Record class. Each instance of the Active Record class represents a row of the table. Even a new column added to the table, the framework automatically detects it and adds to the active record class.

While using ActiveRecord, we need to be careful about the naming convention.Rails uses several naming conventions.

Class and table names : Class name should be singular where as table name should be plural. Also while defining a class, we should use the camel case while naming the class. For example if table name is 'users' , then class name should be 'User'.

Identifier : ActiveRecord automatically finds a column called 'id' and uses it as a unique identifier.The 'id' should be integer type and value should be populated by the database .Staying with these convention saves you some configuration and also makes your code much easier to understand.

Foreign keys : Foreign key should be named as _id.

Lets check with an example ....

class Product < ActiveRecord::Base

end

In the above example , I have defined a class called Product, which inherits the property of the Base class in the ActiveRecord module. From the naming convention we can know that the above class wraps the database table called products . The above information is enough for the Base class to query for all the columns in the products table.

Now check another example ....

class Product < ActiveRecord::Base

belongs_to :category

end

The above definition is very similar to the previous one. But here the line “belongs_to :category” specifies that there are many-to-one relationship between Product and Category (here Category wraps the table categories). The term 'belongs_to' is a method inside 'Base'. Similarly there are lots of method available which help us minimizing the code.

Now let's see how Active Record helps in minimizing the query.

# Find the client with primary key (id) 10.

product = Product.find(10)

Its SQL equivalent is :

SELECT * FROM products WHERE(products.id = 10)

Similarly..

# Find the client with primary key (id) 10.

product = Product.first

Its SQL equivalent is :

SELECT * FROM products LIMIT 1

Like this you can find lots of methods which really reduces the coding pain.

To know more about Active Record and its methods, you can check the following sites.

http://guides.rubyonrails.org/active_record_querying.html

http://api.rubyonrails.org/classes/ActiveRecord/Base.html