Rails Generators and When to Use Them

Learning Rails to replace Sinatra has been a blessing and a curse. While it makes some of the tedious work in Sinatra automatic, Rails is so versatile that it can be confusing to know how to utilize all of the features included and when it’s appropriate to use them.

One of the features built into Rails that I wanted to focus on is the generators. Rails generators make for an easier set up of a new project, but with so many to choose from, it can be difficult to figure out what each one does and which generator is best for your current situation. I decided to do some research to learn more about the generator specifically focusing on model, resource, and scaffold generators.

Model Generator

The first generator that I focused on is the model generator. This is called by running the following code in the terminal after creating a Rails project.

This generates two items for you: 
1.) a file creating an Artist class in your Models folder 
2.) a file in the db/migrate folder to create the Artist table with a name column

The model generator is perfect for when you are not sure about the routes and views that you want to set up for a specific model, but know that you’ll need a table and class for that specific object type.

Resource Generator

The resource generator has been my favorite to work with so far. Much like the model generator, it is run by running the following code in the terminal:

This generates the two files that the model generator gives you along with
1.) An empty Artist controller that inherits from the ApplicationController

2.) An empty views folder for Artist

3.) A full resources call in the routes.rb file in the config folder

With the resource generator, you’re in a perfect spot to customize your rails application- add whichever view files you’ll need, build out the controller to your liking, and you can limit the resources call as you see fit using the only: method.

Scaffold Generator

I was most interested in seeing what the infamous scaffold generator does as many experience rails programmers warn against using it as a beginner (and now I understand why). The scaffold generator is run using:

Once that code is run, the scaffold generator creates everything that the resource generator does except for two main differences-
1.)The Artist Controller is built out including HTML and JSON formatting

2.)The Artist views folder contains 7 files for different views including JSON and HTML files

Example: _form.html.erb

As you can see, the views are built out as well, containing some code I can understand as a Rails beginner, but some foreign concepts as well (form_with and JSON files).

After doing some research on when you might use the scaffold generator, most of what I’ve found actually warns against using it unless you are fairly experienced and only building a basic CRUD application. In reality, most of the applications that developers are building differ from the standard format that scaffold provides and cause developers the hassle of a long process of deleting what they don’t need. Or, in the case of a beginner such as myself, wading through all of the files in confusion.

Trying to read through scaffold files


Like what you read? Give Matt Crowe a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.