Home » Ruby on Rails Active Record

Ruby on Rails Active Record

by Online Tutorials Library

Active Record

Data structures are represented by a hierarchy of classes. Data is mostly stored in relational database tables. There is an essential mismatch between your program’s object view and database’s relational data view. To remove this mismatch, many attempts have been tried. One way to resolve this mismatch was through the use of Object-relational-mapping (ORM) tools. ORM is the mapping of relational database tables to object-oriented classes.

A perfect ORM hides the details of a database’s relational data behind the object hierarchy. In Rails, ORM is implemented by Active Record which is one of the most important components of the Rails library.

An ORM provides a mapping layer between how a database handles its data and how an object-oriented application handles its data. It maps database tables to classes, database table rows to objects, and database tables columns to object attributes. Active Record mainly carries out the mapping process for you. While using Active Record, you have to no longer deal with database constructs like tables, rows or columns. Your application only deals with classes, attributes and objects.

Ruby Active record

Active Record is based on a design pattern created by Martin Fowler. From this design pattern only, the Active Record got its name. Its code works very well even with less number of lines. It is quite easy to use. Active Record Rails application does not need any configuration at all, if proper naming schemes is followed in your database and classes.

There is one more feature of Active Record that makes your work easy, the implementation of a domain-specific-language (DSL). A DSL is a programming language intended to use in a specific problem domain. It allows you to use dynamically generated methods, like to retrieve a record, method find_by_first_name is used.


Active Record Basics

Some of the basics of Active Record are classes, objects and naming conventions.

  • Active Record Classes and Objects
  • Each table in a database is generally represented by a class that extends Active Record base class. By extending Active Record base classes, model objects inherit a lot of functionalities.

    While using Active Records, you don’t have to set up any database connections. It manages all the database connections for an appication. It adds attributes to your class for each of the columns in the database.

  • Active Record naming conventions
  • Active Record uses the CoC (convention over configuration) principle. On following naming convention, you can take advantage of many dynamic features of Active Record without any configuration.


Class and Database

Database table should be named in the plural form and in lowercase of your model classes. For example, if a model class name is Student, then corresponding table name will be students. With the help of this convention, Rails will automatically find the corresponding table to your model class without any configuration code. It even supports plural nouns such as ‘people’ as the plural of ‘person’.

Rails provides a facility where you can add plurals for a model. To define your own pluralization, add code to the config/environment.rb using Inflector.

In some case, if you don’t want to name your database in the plural form, Rails can be configured with singular-named database tables by adding following line to the config/environment.rb :


Next TopicRuby on Rails MVC

You may also like