Rails generators, power from the command line.

Nhon Dang
4 min readJan 14, 2021

For those who have started using Ruby on Rails, there is a lot of “magic” that happens underneath the hood. Rails generators are very convenient commands that give you the power to build out a simple web application within mere minutes. We’ll specifically talk about the generators for models, resources, and scaffold.

Before we go into details about these generators, let’s do a quick crash course review on the notion of MVC. Models, views, and controllers are the conventions or standards of web application development. Simply put, all of the models, corresponding views, and controllers are kept separate from each other to keep code clean, purposeful, and easy to debug. When manually building out a rails application, we would have to create:

  • The model file
  • The database table migration file
  • The model controller file
  • Restful routes or custom routes
  • The matching view pages to restful methods

Now this is only one very simple model object. Imagine trying to build an application with tens or even hundreds of models where each one would need views and controllers. It can get overwhelming and repetitive trying to do everything manually. This is where generators can become a powerful ally to help cut back the amount of work and do the heavy lifting. All of the information from this blog post can be found in the ruby on rails documentation located at this link here https://guides.rubyonrails.org/command_line.html. There are many other useful tips and tricks that can be found here as well.

The first basic generator is the model generator.

This will generate a model file and a database migration file. Syntax for this can be written in a few different ways but they usually follow the same convention. The first portion is typing out ‘rails g model’ with the name of the model, then the fields with their corresponding types separated by colons. Note: a string class will be considered the default class if not specified on a column. An example is listed below

rails g model pet name:string age:integer

Rails model

The model name is “pet” with two attributes being the name and age. The name is a string class and the age an integer. This command is pretty basic but it does a lot of work for you. It will create the pet.rb model file and the table migration file and have everything set up. If you were to take a look into each file, they’ve been set up in a boilerplate configuration. This can be useful if you want to fine tune and customize your models in small portions.

The resource generator provides more features compared to the model generator.

The resource generator shares the same concept as the model generator. Though resource will provide more automation by creating more useful files and routing the model all at once. The syntax is very similar with the only difference being “model” is now replaced with “resource”.

rails g resource Cat name:string breed:string age:integer

Rails resource

As you can see from above, other files including ones you may not even need to touch has been created for you. Rails resource created a views folder and a corresponding cats controller file and its routes. You’ll still have to do some work and put in code that suits the functionality of your application, but it definitely reduces the amount of repetitive typing.

Lastly, scaffold is the most powerful generator of the three.

If you would like to have the most amount of work done for you, rails scaffold is the way to go. This will create the model file, migration files, controller file, routes, and also populate the restful methods within the controller and the individual view pages . As seen in the pictures below, the scaffold will generate all of these for you and it may only require slight tweaking to get everything up and running. I would highly recommend checking out each files to see the magic of scaffold. Syntax follows the same convention as the previous generators.

rails g resource Dog name:string breed:string age:integer

Rails Scaffold

As you can see, these generators are highly useful and can save a lot of time on a new or existing application. Rather than doing everything manually or by hand, this does all of the low level work for you. Within only 10 minutes, you can have a simple website up and running with all of the basic functionality. This is also a perfect environment to learn and experiment with the different behaviors in rails.

--

--