A few days after shifting gears from tedious form-writing in the world of Sinatra to learning about Rails, it has become quite apparent that the “magic” of Rails is there — it’s just a matter of figuring how best to wield the wand and understanding the details of what the platform offers.
One obvious time-saving tool Rails has gifted us: generators to create models, views, and controllers for CRUD actions. Though it seemed like a no-brainer to use migration generators, it also seemed like the other generators and scaffolding received much flack for providing “too much” unnecessary code and files, but I was curious if “too much” could be a good thing in certain scenarios and more importantly, if there exists a general set of guidelines for when to use each.

A few basic things before we begin, including the general syntax for Rails generators…
rails generate [type of generator] NAME
OR
rails g [type of generator] NAME
Model Generator
Command: rails g model Shoes name brand shoe_size:integer
Creates:
- A model in models

- A migration file with timestamp in db/migrate

Resource Generator
Command: rails g resource Shoes name brand shoe_size:integer
Creates:
- A model in models
- A migration file with timestamp in db/migrate
- A shoes_controller.rb file in controllers


- Resources routes in routes.rb, typically found in the config directory

Scaffolding
Command: rails g scaffold Shoes brand shoe_size:integer
Creates:
- A model in models
- A migration file with timestamp in db/migrate
- A shoes_controller.rb file in controllers
- Resources routes in routes.rb, typically found in the config directory
- Seven view files for your RESTful routes (index, create, new, edit, show, update, destroy) with HTML and erb files — plus a json builder file for index and show pages

While it’s advised that beginners build their own MVCs to familiarize themselves with creating each of these files entails, it can also be beneficial to play around with scaffolding to get an idea of what’s available.
I started digging into the list of other generators, too.
Task Generator
Command: rails g task clothing shirt pants
Creation: lib/tasks/clothing.rake
Inside the file:

Assets Generator
Command: rails g assets jacket
Creates these empty templates:
- app/assets
- javascripts/jacket.coffee
- stylesheets/jacket.scss
Scaffold Controller Generator
Command: rails g scaffold_controller umbrella
Creates:
- Front-end templates
- Scaffolded controllers
- No models!
Mailer Generator
Command: rails g mailer Example
Creates:
- A mailer
- Blank mailer test
Migration Generator
Command: rails g migration CreateClothes
Creates a timestamped migration file. If you need to add an extra column, you can do so through this command as well.
Command: rails g migration add_size_to_clothes size
I saved the best for last…. The undo button.
rails destroy
OR
rails d
This will delete all the files that were previously created using the generator.
- rails d model Shoes
- rails d controller Shoes
- rails d scaffold Shoes


