DEV Community

Cover image for Understanding Active Record
Shaher Shamroukh
Shaher Shamroukh

Posted on

Understanding Active Record

What is Active Record?

Active Record is the M in MVC - (the model) - which is the layer that is responsible for representing business data and logic.
So Active record is an Object Relational Mapping system.

What is Object-Relational Mapping?

Alt Text

Object-relational mapping (ORM) libraries map database tables to classes that's it.
So if a database has a table called orders , the program will have a class named Order .
The rows in this table correspond to objects of the class.
a particular order is represented as an object of the Order class.
Within that object, the attributes are used to get and set the individual columns.
Also the Rails classes that wrap our database tables provide a set of class-level methods that perform table-level operations.

For example, we might need to find the order with a particular ID. This is implemented as a class method that returns the corresponding Order object. In Ruby code, that might look like this:

order = Order.find(1)
puts "Customer #{order.customer_id}, amount=$#{order.amount}"
Enter fullscreen mode Exit fullscreen mode

Also these class-level methods return collections of objects:

Order.where(name: 'logan').each do |order|
puts order.amount
end
Enter fullscreen mode Exit fullscreen mode

So basically an ORM layer maps tables to classes, rows to objects, and columns to attributes of those objects.
Class methods are used to perform table-level operations, and instance methods perform operations on the individual rows.

Active Record

Active Record is the ORM framework that comes with Rails.
It closely follows the standard ORM model:
tables map to classes, rows to objects, and columns to object attributes.
But It differs from most other ORM frameworks in the way it’s
configured.
By relying on convention and starting with sensible defaults,
Active Record minimizes the amount of configuration that developers perform.

Let's have an example, here’s a program that uses Active Record to wrap our orders table:

require 'active_record'
class Order < ApplicationRecord
end
order = Order.find(3)
order.pay_type = "Purchase order"
order.save
Enter fullscreen mode Exit fullscreen mode

This code uses the new Order class to get the order with an id of 3 and modify the pay_type .
As you see Active Record relieves us of the hassles of dealing with the underlying database, leaving us free to work on business logic.
and Active Record does way more than that.

Active Record Naming Conventions

By default, Active Record uses some naming conventions to find out how the mapping should be done.
Rails will pluralize the class names to find the respective database table.
So, for a class Book, you should have a database table called books. The Rails pluralization mechanisms are very powerful, being capable of pluralizing singularizing both regular and irregular words.
When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the CamelCase form, while the table name must contain the words separated by underscores.

Here is an example from the rails guide
Alt Text

Conclusion

Active Record integrates seamlessly with the rest of the Rails framework.
Active Record supports
sophisticated validation of model data, and if the form data fails validations, the Rails views can extract and format errors.
Active Record is the solid model foundation of the Rails MVC architecture.

I hope you have enjoyed reading the article as i have enjoyed writing it!

Please have a look at Rails guide

Top comments (0)