Prev Source
Next Source

Active Record Basics

original source

Active Record is the M in MVC - the model - which 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

Active Record was described by Martin Fowler in his book Patterns of Enterprise Application Architecture

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

Object Relational Mapping, commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system

When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. This is particularly true for ORM frameworks in general. 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. The idea is that if you configure your applications in the very same way most of the time then this should be the default way

By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class Book, you should have a database table called books

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 use the snake_case form


Date
March 22, 2024