
Once in a Generation:
So, you just recently discovered rails and just love love love the magic of generators, huh? With a single command line you can create migrations, models, controllers, resource routes. Whoah! If Rails were your nearby Shake Shack, we could say you’ve just gotten a tasty burger made and ready to eat— and in no time at all! But, if you don’t properly examine the menu, you might not realize what exactly you’re ordering…
Lets take a look at our menu by typing just the bash command ‘rails g’ to pull up our menu:

Shown in the menu are the basic generators that Rails provides for us. Additionally we can see the following flags and their uses:
Help (-h) will print what the generator creates and its proper usage.
Pretend (-p) will execute the generator and we can see all associated actions that follow in terminal — however the files will not actually be created and saved. This is useful if we’re ever unsure of the generator and perhaps -h is not satisfactorily explicit.Force (-f) will override any already existing files with new files created by the generator action.Skip (-s) will skip creating any files that already exist so that we don’t duplicate our efforts. We can also use --skip to do things like “--skip-migration if perhaps we specifically do not want to create a certain kind of file.Quiet (-q) will execute the generator without any status notifications printing
In the rest of this post, we’ll be breaking a few of the most useful generators down and checking out what they offer.
The most common generator we use is the migration. This creates a stub database migration class in our “db/migrate” directory. If we want the model and the corresponding migration, it is best to proceed up the chain to model: Generating a model creates a model.rb file, a test file and .yml fixture and a migration for said model.
rails generate model account
Would generate the following:
app/models/account.rb
test/models/account_test.rb
test/fixtures/accounts.yml
db/migrate/XXX_create_accounts.rbGenerating a controller, although often discouraged by many, also generates a JavaScript file (or CoffeeScript file if the coffee-rails gem is in the Gemfile) and a Cascading Style Sheet file (or SCSS file if sass-rails is in the Gemfile) for that controller.
`rails generate controller CreditCards open debit credit close`Would generate the following:
CreditCards controller with URLs like /credit_cards/debit.
Controller: app/controllers/credit_cards_controller.rb
Test: test/controllers/credit_cards_controller_test.rb
Views: app/views/credit_cards/debit.html.erb […]
Helper: app/helpers/credit_cards_helper.rbIf, instead of just a model simple controller, you know that what you want to generate a full resource route and structure, generating a resource will create an empty model and corresponding controller as well as default restful routes in our “config/routes.rb” file. As Rails notes however, “Unlike the scaffold generator, the resource generator does not create views or add any methods to the generated controller.”
Generating scaffolding Scaffolds an entire resource, from model and migration to controller and views, along with a full test suite and resource routes.
Generating a mailer creates a mailer class in the app/mailers directory and invokes your template engine and test framework generators.
rails generate mailer Notifications signup forgot_password invoiceWould generate the following:
creates a Notifications mailer class, views, and test:
Mailer: app/mailers/notifications_mailer.rb
Views: app/views/notifications_mailer/signup.text.erb […]
Test: test/mailers/notifications_mailer_test.rbThese are just a few of the many available generator and, as the Rails Guide even notes:
You can install more generators through generator gems, portions of plugins you’ll undoubtedly install, and you can even create your own custom generators!
WHOAH! RAILS G!

