1 What is Active Record?

Active Record is the M in MVC - The model

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

1.1 The Active Record Pattern

In Active Record, objects carry both persistent data and behavior which operates on that data.

Active Record takes the opinion that ensuring data access logic as part of the object will educate users of that object on how to write to and read from the database.

1.2 Object Relational Mapping

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.

1.3 Active Record as an ORM Framework

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.

2 Convention over Configuration in Active Record

When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code.

However, if you follow the conventions adopted by Rails, you'll need to write very little configuration (in some cases no configuration at all) when creating Active Record models.

2.1 Naming Conventions

  • Model Class - Singular with the first letter of each word capitalized (e.g., BookClub).
  • Database Table - Plural with underscores separating words (e.g., book_clubs).
Model / ClassTable / Schema
Articlearticles
LineItemline_items
Deerdeers
Mousemice
Personpeople

2.2 Schema Conventions

Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns.

  • Foreign keys

    singularized_table_name_id (e.g., item_id, order_id)

  • Primary keys

    This column will be automatically created.

    (bigint for PostgreSQL and MySQL, integer for SQLite)

    |bigint |8 bytes | large-range integer | -9223372036854775808 to 9223372036854775807|

3 Creating Active Record Models

It is very easy to create Active Record models. All you have to do is to subclass the ApplicationRecord class and you're good to go:

Suppose that the products table was created using an SQL (or one of its extensions) statement like:

Schema above declares a table with two columns: id and name. Each row of this table represents a certain product with these two parameters. Thus, you would be able to write code like the following:

4 Overriding the Naming Conventions

What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions.

5 CRUD: Reading and Writing Data

Create, Read, Update and Delete

Active Record automatically creates methods to allow an application to read and manipulate data stored within its tables.

5.1 Create

The create method call will create and save a new record into the database:

Using the new method, an object can be instantiated without being saved:

A call to user.save will commit the record to the database.

5.2 Read

Active Record provides a rich API for accessing data within a database.

5.3 Update

Once an Active Record object has been retrieved, its attributes can be modified and it can be saved to the database.

A shorthand for this is to use a hash mapping attribute names to the desired value, like so:

5.4 Delete

Likewise, once retrieved an Active Record object can be destroyed which removes it from the database.

If you'd like to delete several records in bulk, you may use destroy_by or destroy_all method:

Thank You 😼