Sitemap
Dimitri Chenet

Dimitri Chenet

Ruby With Active Record

2 min readNov 17, 2021

--

ActiveRecord is a library that provides an Object-Relational Mapper (ORM) for Ruby. It is very useful when we talk about managing data and relationships between models.

This blog post will explore Ruby with and without ActiveRecord with models, as well as the advantages of foregoing its use in certain circumstances.

What is ActiveRecord?

ActiveRecord is a Ruby library that sits between the database and logic code.

Benefits of using Ruby with ActiveRecords

  • Flexible
  • Consistent
  • Productive
  • Easy to manage changes
  • High performance

Ruby with ActiveRecord

Get Dimitri Chenet’s stories in your inbox

Join Medium for free to get updates from this writer.

The ActiveRecord gem is the backbone of most Ruby applications. It was created to help the developers speed up their production time and not have to excessively code things that can be handled dynamically by active record.

Active record will Represent models and their data, represent associations between these models, Represent hierarchies through related models, Validate models before they get persisted to the database, and perform database operations in an object-oriented fashion.

Being conscience about the way we name our data in Active record is highly apparent while working with it in ruby, we must make sure to make certain items in our data plural vs singular if not we will get several error messages.

We create migrations and this is where we create the format for our tables with Active Record. There are several data types that we can include in our table such as strings, integers, booleans, timestamps and foreign keys.

The Schema is a section where the format for our table will be presented separate from the migration page. It creates a clear way to look at our foreign keys and the primary keys, foreign keys are sections in our table that keeps track of id names from other tables that correlate with the table that is connected to all other tables.

The Seeds section is where we can hardcode information or have them dynamically made up with an app gem called faker that will create whatever information we need such as names, dates, etc at scale.

Active record in our models section is where we define methods to manipulate the data that has been imported from our seeds file.

Active Record also uses C.R.U.D= Create, Read, Update, Delete

Create-Active Record objects can be created from a hash, a block, or have their attributes manually set after creation. The new method will return a new object while create will return the object and save it to the database.

Read- Active Record provides an expansive API for accessing data within a database.

Update- Once Active Record object has been retrieved, its attributes can be changed and it can be saved to the database.

Delete- Once retrieved an Active Record object can be destroyed which removes it from the database.

--

--