# Active Record ORM

  • Introduction

    Active Record is the M in MVC (opens new window) - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.

    Object Relational Mapping (opens new window), commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system. Using ORM, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements directly and with less overall database access code.

    Active Record gives us several mechanisms, the most important being the ability to:

    • Represent models and their data.
    • Represent associations between these models.
    • Represent inheritance hierarchies through related models.
    • Validate models before they get persisted to the database.
    • Perform database operations in an object-oriented fashion.
  • 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.

    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'.