Ruby on Rails Generators (and how to undo them)

Gianpaul Rachiele
5 min readAug 17, 2017

Generators in Rails have a lot of power and can save you a lot of time. They can also create a bit of a mess if you don’t which use each one serves. Below is a list of rails generators that you can make use of.

Usage: rails generate GENERATOR [args] [options]
General options:
-h, [--help] # Print generator's options and usage
-p, [--pretend] # Run but do not make any changes
-f, [--force] # Overwrite files that already exist
-s, [--skip] # Skip files that already exist
-q, [--quiet] # Suppress status output

Please choose a generator below.

Rails:
assets
controller
generator
helper
inherited_resources_controller
integration_test
mailer
migration
model

observer
performance_test
resource
responders_controller
scaffold
scaffold_controller
session_migration
task

I’ve highlighted the four generators I’ll cover in this post.

rails new

Running the rails new PATHterminal command will give you the bare bones of a rails project. The first thing that needs to be done is to create a directory that will house your Rails app.

Next you run the command after navigating into the newly created directory and run the rails new command with the name of your app in place of PATH you will see that Rails automatically generates a folder bearing the name of your new app and tons of files inside.

This is not all the files. Just a snippet.

For any of you who have done work with web apps before and haven’t used rails you’re probably thinking…

Ruby on Rails stresses the following principles:

guides.rubyonrails.org

The rails new command starts you out with a basic configuration and some boilerplate code that adheres to the above principles.

rails generate scaffold

Let’s first talk about one of the rails generators that you should probably avoid, scaffold , since it will create a lot of extra stuff that you generally don’t need. The scaffold generator creates a full set of a model, a database migration for that model, a controller to manipulate it, views to view and manipulate the data, and a test suite for each of the things listed before. This doesn’t give you much flexibility in how you want things to interact since it’s all being created for you and there’s a chance something could go wrong if you don’t follow the proper naming conventions for Rails.

rails generate controller

The rails generate controller controller_name terminal command generates the view files, test files, and the controller files needed for the newly created controller.

This command does not create the model files needed for your application and also doesn’t create any migration files to create tables in your database for you but rails does have commands for that!

rails generate model

The rails generate model NAME fields command creates our migration for us to create a corresponding table in our database (even pluralizing to keep with conventions!) and creates the fields based on what you pass in after the name of the model. See the example below.

The command also creates the test files and creates a directory for the views for the model but falls short of creating any views which may or may not be what you need for your task. What if you forgot to add a column into your table or you want to create a new table in your database after already creating the model manually? Rails has a command for that!

rails generate migration

You can create a migration using the above command. If you follow some conventions Rails with try to fill in the migration for you though it might be easier to just create a migration and fill in the extras yourself. Running the following command:

Generates the following code:

As you can see Rails can be powerful if given information in the correct format.

Undoing Generations with rails destroy

If you accidentally create a model, migration or controller it can be undone by running the command rails destroy accidentally_created_thing . As a side note both generate and destroy can be substituted with just the first letters of each of the word, g and d. See the example below where we undo the migration we created above.

As you can see Rails removed file associated with the migration named create_dogs .

Now that you know about generator and destroy enjoy the magic of Rails.

Sources:

--

--