DEV Community

Connor Vosberg
Connor Vosberg

Posted on

Ruby on Rails - Generators

Introduction

In Ruby on Rails, writing the same boilerplate code over and over again can become repetitive, time consuming, and error-prone. Luckily, Rails offers a great solution called 'generators' which streamlines the process of creating essential components, such as models, controllers, migrations and more. In this post, we will discover how to use Rails generators as well as how they improve development efficiency and accuracy.

Before we dive into the most common uses of rails generators and how to use them, let’s go over some important guidelines to use these powerful tools to the best of their ability.

  • Naming Conventions: Rails follows specific naming conventions for all of its components, so it's important to keep in mind what rules go with which type of generator you are using.
  • Generator Options: Rails generators come with extra options that allow you to further customize the code that is generated. For example, you could add specific attributes to a model when writing it in the terminal as opposed to defining them after the model is generated.
  • Documentation and Help: You can access specific documentation or help for a specific generator by running rails generate generator_name --help. For example, running rails generate model --help will return all the necessary information that goes along with generating a model.

To set up Rails generators, you first need to create a new Ruby on Rails application. You can do this quickly by entering the following command into your terminal:

rails new YourAppName
Enter fullscreen mode Exit fullscreen mode

You can then navigate into your applications root directory and run bundle install, which will read the Gemfile for your project and install all of the required gems and their dependencies. Since generators are an important part of Rails, they will be automatically available once your application is set up. Finally, with your Rails app set up and gems installed, you can now make use of Rails generators to create various components of your application.

A few of the most common generators used in rails and the ones we will go over here, are for generating a model, controller, migration, or a resource.

Model

Let’s start off with the model generator. Generating a new model will simply just create a new model as well as the migration file for creating the corresponding database table with the specified attributes. When generating a new model, type the following into the console:

rails g model ‘ModelName’ attribute1, attribute2:integer
Enter fullscreen mode Exit fullscreen mode

You can use rails g or rails generate for the first portion, followed by the type of thing you want to generate, in our case, a model, followed by the ModelName, in its singular form, followed by the attributes associated to the model. When adding our attributes, everything will default to the data type of string unless it’s specified otherwise in the format: attribute:data_type

Here’s an example of a generated Hike model: rails g model Hike name difficulty:integer location
Model:
Hike Model
Migration:
Hike Migration

Controller

The next generator we're going to cover will be the controller generator. If we wanted to generate a new controller for a Hike model we run the following in the terminal:

rails g controller Hikes
Enter fullscreen mode Exit fullscreen mode

When generating a controller, we use a model's name in the pluralized form. This command will generate a file called 'hikes_controller.rb' in the 'app/controllers' directory. This file contains the code for the HikesController which is responsible for handling various HTTP requests and responses.

Here’s what our HikesController would look like after it is created:

Hikes Controller

Migration

The next generator we will cover is going to be the migration generator. Migrations are a way to manage your database over time and update or remove data when necessary. To generate a migration, you run the following in your terminal:

rails g migration NameOfMigration
Enter fullscreen mode Exit fullscreen mode

The name of your migration should reflect the changes you’re making to the database schema. For example, if we wanted to add a length attribute to Hikes, we could run:

rails g migration AddLengthToHikes
Enter fullscreen mode Exit fullscreen mode

When this command is run, a new migration file will be created in the ‘db/migrate’ directory with a timestamp and the name that was provided. So our file from above might look something like ‘20230922192325_add_length_to_hikes.rb’. You can then open the migration file and define the changes you wish to make. You can use various methods in the migration file to make changes to the schema. Some common methods include, add_column, remove_column, create_table, drop_table, etc. For our purposes, we will focus on add_column. Here’s an example of adding a length column to the hikes table:

Add Length to Hikes

Finally, to apply the change you’ve made and to update the schema to reflect those changes, you would run rails db:migrate in your terminal.

Resource

Last but not least, we are going to cover the resource generator, one of the most powerful generators. Upon generating a new resource, many new files are created including the Model, Controller, Migration files like we've seen above, as well as an updated the ‘config/routes.rb’ directory with RESTful routes for the resource, including index, show, create, update, and destroy.

To generate a resource for Hike, you would run the following in your terminal:

rails g resource Hike name difficulty:integer location length:integer
Enter fullscreen mode Exit fullscreen mode

After running this command, you will have access to all of the files defined above as well as a RESTful routes for the resource as seen below:

hikes routes

All in all, using generators is a very helpful and powerful resource to use when creating a Rails application and it can increase productivity, decrease repetitive code, as well as reduce possible errors.

Top comments (0)