Active Record and Generators

Justin Tollison
Programmer’s Journey
6 min readDec 24, 2021

Hey guys, it’s been quite some time since my last blog post and a lot has happened since then. The Flatiron courses are going well, albeit it has become a bit more difficult now that we’re learning Ruby on Rails and data basing with SQL. I thought I’d give a quick run-down of Active Record, though I’m still a beginner so don’t take my word as law. Well, let’s get down to it!

So what is Active Record? Well, checking the documentation yields this definition:

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.

What that means is, it essentially handles all of the backend logic of your database. And to go further, ActiveRecord actually has a way to handle creating tables, associations and routes through generators when you first create your database. For my own current project, we use postgresql, but there are other numerous SQL data basing that you can use. To continue, Active Record uses object relational mapping. And again, here is the official definition from the Ruby on Rails documentation:

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.

In more simple terms, when you create an object, it’ll be connected to the database that you’re creating it on. If I were to have an empty Dog table on my database with columns such as breed, color and size then I could create an object with those traits and through ORM the object will be stored or connected to the database.

Using ORM, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements directly and with less overall database access code.

And if I were to have associations or relations between two tables, such as a Users table, then I could easily write this within Active Record without having to interact with the SQL database separately. Active Record is amazing in this in that it handles a lot of the logic for you!

To skip ahead a bit through the basics, Active Record has generators that you can write in your terminal while in the directory of your Ruby on Rails project. The generators will create the schemas and migrations and push them to your SQL database, as well there are generators to create the routes and models. To begin with, let’s create a few migrations for a new SQL database!

When creating your first migration, you will want to follow the naming conventions. Again, here is more documentation explaining the naming convention when using the generators.

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. The Rails pluralization mechanisms are very powerful, being capable of pluralizing (and 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 use the snake_case form.

In short, for Active Record to detect how to use its ORM with your models, migrations and so on with your database, you must follow the naming conventions. Of course, there is also a way of overriding this if your database has to follow a different naming convention. That is explained in more detail on the website. For now, let’s take a look at our schema before we push the migration onto the database.

As you can see, Active Record creates a class of CreateDogs which holds a function that will create a dogs tables with the columns of breed, color and size that hold string values. There is also a timestamps column that gets added to every table by default. I recommend always looking through your schema before pushing the migration as you’ll be able to doublecheck if the data is correct. It is much easier to delete a migration now and edit it instead of having to go within the SQL database. Active Record also provides the migration generator with an add_column and such to edit the table, but it’s much less hassle to get it right the first time. If you want to delete a migration before pushing it, use d for delete instead of g.

This will delete the migration and you can recreate your table. Now, if everything looks good we can now migrate the table onto our database using rails db:migrate and once that is pushed you can now work on generating the models.

rails g model Dog

Following the naming convention, you want the model name to be capitalized. This will generate a class model for Dog that’s connected with your migration. Within your model class, you’ll be able to use features such as validations, conversions and associations. has_secure_password is an important example, it provides a way to securely store any password in an encrypted form for something such as a web app that requires a login. In an event that someone gains access to your database, has_secure_password within the model will encrypt the data on the table that the model is connected to. Now let’s move onto controllers.

rails g controller dogs

Again, this must follow the naming conventions. As appose to the generator for model requiring capitalization, for controllers, the name must be plural and lower-case, similar to when you create a migration. Citing the documentation again, here’s what the controller does.

Action Controller is the C in MVC. After the router has determined which controller to use for a request, the controller is responsible for making sense of the request and producing the appropriate output

Within the controller will be methods and actions that determine where the routing will lead to. Common routes are #show and #index which in the Controller class will be shown as:

And to show the related routes in your routes file:

The routes should look familiar as they are Get requests from the URL that you enter in the browser. In your local environment it will commonly be a localhost followed by the route.

It’s very exciting to see how Active Record handles the generators and connects everything through naming conventions. It handles a lot of the logic for you so you aren’t scrambling within the SQL database, making the backend work for your web app a lot more smoother and streamlined. Of course there is much more to Active Model and Active Controller than what I covered, but I just wanted to give an idea of how it all worked. And now, you’re probably wondering how tedious it would be to repeat this process for every table! Well, they thought of that. Here’s one line of code that uses the generators to generate the migration, the model, the controller AND the routes all in one!

This generator will create everything all at once and connect it all, saving you a lot of work. But anyways, thanks for reading if you’ve reached this far and I apologize for any errors or inconsistencies!

--

--

Justin Tollison
Programmer’s Journey

Unity Game Developer and Flatiron Software Engineering Alumni