Common Rails Generators

Kimberlyn Stoddard
3 min readSep 2, 2020
Photo by Michael Wright on Unsplash

Ruby on Rails is a Ruby library that has a lot of useful methods and generators. However, these can only be used if you 1. Know about them, and 2. Know what information you need to put in to use them. Plus, as a bonus, it helps if 3. You know what they actually do. So this blog post will go over some basic Rails generators you can use to get an application up and running.

Photo of Generator

Migration Generator and Model Generator

The first two generators I want to talk about is the standard migration and model generators. The syntax for utilizing these generators is extremely similar. Let’s say we want to build a table for dogs, which takes a string argument for name and breed and an integer argument for age. To use the migration generator, you’d input the following:

rails g migration create_dogs name:string breed:string age:integer

or, to use the model generator, you would put:

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

The migration generator will create a migration file in the db/migrate folder (per Rails conventions) and have it inherit from ActiveRecord. It will not, however, create the model for you. The model generator will create both. As a note, the arguments default to strings, so writing the following would have worked just as well:

rails g migration create_dogs name breed age:integer

The migration generator can also be used to add columns and delete columns, just by inputting the name in the correct format. For instance, if we wanted to add an owner_id argument, our code would look like this:

rails g migration AddOwnerIdToDogs owner_id:integer

Controller Generator

The controller generator is very similar to the migration generator in that it will only generate one thing… the controller. The syntax for this is, to keep with our dogs example and assuming we want to have an index, show, new, name, and create method:

rails g controller Dogs index show new create name

This will build the dogs_controller.rb file and pre-build empty methods with those names. As a note, it will NOT build in the routes for you.

Finally, the last generator I want to talk about is the Scaffold Generator.

The Scaffold Generator will prebuild pretty much everything the generators listed above will and more. It will create your migration file, your model file, your controller file, and it will also build the routes for all seven of the basic RESTful actions.

The syntax is the following:

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

So, if it does all that work for you, why don’t we use it? Because in programming we try to only build what we need and the scaffold generator will build SO MUCH MORE than what we’ve listed above. It basically loads down your application with a bunch of code you’re not going to use and don’t really need. Is it fast? Yes. Is it efficient? For you. But unless you really need everything it builds, it’s best practice to keep your code cleaner.

--

--

Kimberlyn Stoddard

Student at FlatIron School learning Software Engineering