Active Record (Rails Model) - Introduction
Tags: rails, ruby, database
Published: 2021-07-30 22:16:00

# Content List:

1. [Rails Arhitecture](#rails-arhitecture)
2. [What is Active Record?](#what-is-active-record?)
3. [The Active Record Pattern](#the-active-record-pattern)
4. [The Object Relational Mapping (ORM)](#the-object-relational-mapping-orm)

# Rails Arhitecture

Rails is a web-application framework that includes everything needed to create database-backed web applications according to the **Model-View-Controller (MVC)** pattern. and in this series we are going to speak about Model layer.
 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kh9fll0f9hb0d30rs543.png)

# What is Active Record?

[Active Record](https://github.com/rails/rails/tree/main/activerecord) is a ruby's gem. and it is the M in MVC - the Model - which represents _business data_ and _logic_. It is an implementation of the [**Active Record Pattern**](#the-active-record-pattern) which itself is a description of an [**Object Relational Mapping (ORM)**](#the-object-relational-mapping-orm) system.


# The Active Record Pattern

In the Active Record Pattern every record in the database is represented by an object, this object will serve us with:
1. Wraping a row in database table.
2. Encapsulating the database access.
3. **Attributes:** Adding table's fields as object's attributes.
4. **Behaviors:** Adding domain logic (methods) on that data.

So the objects will carry both persistent data and behavior which operates on that data. And each Active Record object has **CRUD** (**C**reate, **R**ead, **U**pdate, and **D**elete) methods for database access. This strategy allows simple designs and straight forward mappings between database tables and application objects.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thykum4y226xxpv84ysu.png)
So some of the basics of Active Record are **classes** and **objects**.

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

# The Object Relational Mapping (ORM)

A technique that connects the rich **objects** of an application to tables in a relational database management system. so ORM lets you query and manipulate data using Object-Oriented programming.

## Active Record as an ORM Framework

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wk3rxfn5dw6wm5evh5i3.jpg)
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.

# Resources

[Ruby on Rails Guide](https://guides.rubyonrails.org/active_record_basics.html)
[Martin Fowler](https://www.martinfowler.com/eaaCatalog/activeRecord.html)
[Tutorials Point](https://www.tutorialspoint.com/ruby-on-rails/rails-active-records.htm)
[Java T Point](https://www.javatpoint.com/ruby-on-rails-active-record)
[Active Record Gem](https://github.com/rails/rails/tree/main/activerecord)