Rails Generators

jrrohrer

Jessie Rohrer

Posted on May 19, 2021

Rails Generators

Photo by Rob Pumphrey on Unsplash

When I started learning Rails a few weeks ago, the concept of using generators scared me. I am a visual learner, and I like to have information laid out neatly in front of me. I can't push to GitHub without refreshing my repo page to make sure the push actually...pushed. So imagine my anxiety when I ran a generator for the first time and about 500 files were added to my project.

If you are a beginner who is just as weary of new command line actions as I am, I am here to tell you it's totally OK to embrace Rails' generators. They will make your life easier, I promise. I'm going to break down what the most commonly used generators do, what they add to your project, and how to decide which ones to use and when.

Scaffold Generator

Syntax:

rails generate scaffold ModelName column_name:data_type

This one is basically cheating, but your app will be up and running (if not looking super bland) in just a couple of minutes. But it is unlikely that the code generated will be a perfect fit for your application, because it basically generates a complete app, minus the content. You will end up removing files and taking extra time to go through and figure out which of the generated files you actually need.

When You Should Use It

If you need an app up and running, like, yesterday, and you don't care about bloat files.

Resource Generator

Syntax:

rails generate resource ModelName column_name:data_type

Once you have your project planned out, this generator will save you a lot of time getting set up. It will create your model, controller, migration, helper file, scss file, and views folder. It will also add a full resources call in your routes.rb file. You can specify attributes that will be added to your migration table, and you can specify a 'belongs_to' Active Record association that will automatically be filled in for you.

When You Should Use It

If you want to create your views on your own, you are using a front-end MVC framework, or you are building an API.

Model Generator

Syntax:

rails generate model ModelName column_name:data_type column_name2:data_type2

This generator is great for creating the core code needed to create a model and associated database table without adding a bunch of extra stuff to your program.

When You Should Use It

Primarily when you are adding a new model to your project, if you only want the model file and the migration file.

Controller Generator

Syntax:

rails generate controller ControllerName controller_action controller_action

This command will allow you to create a controller. You can append the names of controller actions you want to add, and Rails will create routes to each of those arguments. It will also create a new view directory and a template file for each of the controller actions specified. Finally, it will also create a helper file and an scss file.

When You Should Use It:

This generator is ideal if you want to create a static views or non-CRUD related features. Like adding a Sessions controller for handling signups and logins.

Migration Generator

Syntax:

rails generate migration MigrationName column_name:data_type

Like the name implies, this generator will create a new migration for you, and add it to your db/migrate file. You can specify the name of the migration and any attributes you want to add. Rails is even kind enough to infer from the name of your migration what you want to accomplish. So if you name your migration AddContentToComments it will create a migration that will add a column called content to your comments table.

When You Should Use It

When all you need is a new migration file.

Notes on Rails Generators

  • You can shorten the command by swapping the word generate with the letter g: rails g model User username:string password:password_digest

  • If you don't want the included Rails test framework, append the flag --no-test-framework to the end of your generate command.

  • If you ever want to know what generators are available to you, you can always type rails generate into your console. You'll get an output that looks like this:

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:
  application_record
  assets
  benchmark
  channel
  controller
  generator
  helper
  integration_test
  jbuilder
  job
  mailbox
  mailer
  migration
  model
  resource
  scaffold
  scaffold_controller
  system_test
  task

ActiveRecord:
  active_record:application_record

TestUnit:
  test_unit:channel
  test_unit:generator
  test_unit:install
  test_unit:mailbox
  test_unit:plugin
Enter fullscreen mode Exit fullscreen mode

Rails generators are your friend. They do a lot of grunt work for you so you can get started on the fun parts of your project.

💖 💪 🙅 🚩
jrrohrer
Jessie Rohrer

Posted on May 19, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related