26
Active Record (Rails Model) - Introduction
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.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--tCq_1ZvH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kh9fll0f9hb0d30rs543.png)
In the Active Record Pattern every record in the database is represented by an object, this object will serve us with:
- Wraping a row in database table.
- Encapsulating the database access.
- Attributes: Adding table's fields as object's attributes.
- 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 (Create, Read, Update, and Delete) methods for database access. This strategy allows simple designs and straight forward mappings between database tables and application objects.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--JUGMqKo2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thykum4y226xxpv84ysu.png)
- 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.
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.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--9TJhthqK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wk3rxfn5dw6wm5evh5i3.jpg)
- 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.
26