Active Records In Ruby on Rails

Active Record – Active Record is the model (M) in MVC which represent business data and logic. It supports object Relation Mapping, i.e. the object of the business logic which needs persistent storage in database.

Object Relation Mapping is a technique that maps the object to the database relations(tables). This helps to reduce the sql statements needed to access the database as it automatically maps the relation structure in object and the object’s data in the relation.

Convention over Configuration 
Rails follow certain convention which helps to reduce configuration. It helps to save time and overhead of excessive code gets reduced. Following are some convention followed by rails.

  • Naming Conventions – Rails uses some naming conventions to determine the mapping between models and database tables. Following are the naming conventions.
        Table Name – Plural with underscores separating two words. example  user_articles, person

      Model Name – Singular without underscore with first letter of each word capital. example UserArticle, People

 
  • Schema Convention – Rails follow some conventions for schema as well. Some column names should be according to the convention based on the purpose of the column.

        Foreign Keys –  The syntax for a primary key is “table_name_id”, example order_id, article_id. The key point here is to note that the table name should be singular.

        Primary Keys – By default “id” is used as primary key which is an auto-incremented column that gets created automatically.

Leave a Reply

Your email address will not be published. Required fields are marked *