A Recollection of Active Record

Tomazye’ anderson
4 min readApr 5, 2020

As developers we’re all about making things as easy as possible so that we can build amazing applications. The amazing ActiveRecord Ruby gem is one of the things that contributes to this. It’s a Ruby gem which means we get an entire library of code simply by running “gem install activerecord” or by including it in our Gemfile. If we didn’t have ActiveRecord we’d have to maintain custom code for each project we build and worry about database connectivity rather than the application itself. If you have multiple moving parts this can quickly become difficult and repetitive.

Active Record is an ORM. An ORM or (Object-Relational-Mapping) is basically accessing a relational database using an object-oriented programming language. What this is doing is giving us a way to connect database tables to classes and instances of those classes to rows in the tables. Two very important reasons why we use an ORM is to one, cut down on repetitious code and make it “DRY”(Don’t Repeat Yourself). The other is to implement conventional patterns for organization and readability. These become very important as you begin to work with other programs like Rails and so forth.

Once our Gemfile knows to use ActiveRecord we have to tell it where the database is located that’ll be connecting to. We can accomplish this by running “ActiveRecord::Base.establish_connection”. Once the connection is established we can connect our database. From there you would create a table since the database is empty.

Say we created a “students” table, the last step would be to link it to our Students model class so that it makes use of ActiveRecord’s built-in ORM methods. In order for us to do this we use what’s called “Class Inheritance”. We’ll be making our Student class a subclass of “ActiveRecord::Base”.

Our Student class now lets us communicate to the students database. It’s also gained many new methods because of its inheritance relationship with ActiveRecord.

One other very important tool that expedites the process of getting your ActiveRecord gem up and running with your database is “Rake”. Rake was developed as a task management tool for Ruby. It’s very useful for things like creating databases and maintaining certain files within that database. We use “Rake tasks” to execute and automate certain jobs. Once we define a task we can execute it right in the terminal. For example writing “rake -T” will print out a list of available rake tasks with their description.

Rake db:migrate- will create a new table for us in the database. Doing this we are “migrating” the database by applying SQL statements that alter that database.

Rake db:seed- will populate our database with some dummy data. In order to seed you database you need to have a seeds.rb file within the db directory. This code will create instances of your class which were defined in the seeds.rb file.

Rake console- will start up the pry console. Here you can check to see if you in fact seeded the database correctly(ex: Student.all ).

Active Record also allows us to create associations through our models using ActiveRecord macros. It takes a good amount of code to associate our models but using macros with ActiveRecord makes it quicker and much more manageable. We mainly use the belongs to, has many, and has many through associations but there’s also has_one, has_one :through, and has_and_belongs_to_many. Using these macros writes code for us similar to metaprogramming.

This was a basic overview of Active Record and the many things it can do once put to use. This gem is very important and essential in the lifetime of a programmer.

--

--