Introduction to Rails Generators
Bored of re-writing the same code over and over? You’re a programmer, of course you are. This introduction to generators in rails gives you a brief overview of their powers and what to watch out for!
Rails generators are a shortcut to building your MVC framework and database, which can become extremely repetitive. Below is a list of the generators I have found to be very useful, how to call them and what tangible files they provide you with. In the images below g stands for generate and d stands for destroy (both can be used, ruby doesn't care)
MIGRATION

What you get…

As you can see the table columns are not provided so these must be remembered and manually filled in. They can be called in the terminal as shown in the other example…

The key part of this call is the ‘Add’ ‘To’ and ‘Ride’. Rails needs these que’s to understand what you are trying to achieve.

MODEL

This generator will get you a migration just like above, however it will have the table columns added in for you! It also creates a model file as seen below. Model relationships must be added.

CONTROLLER

Everything after ‘Artists’ is optional and dependant on your apps needs. A controller generator will create the ArtistController, all views specified and routes. One advantage this generator has over the resources generator is that our views files are created for us.

The main problem with the Controller generator is that all our routes are created as GET’s. These can be manually changed and is still quicker than hard-coding everything it has created. This leads us to our last generator which handles this problem.
RESOURCE

Finally the resource generator, this generates the most code. It provides the migration, model, controller, views (folder) and routes files. Specifically generating all routes GET, POST, PATCH and DELETE. It also does it in a much more DRY and cleaner way as shown in the one line below.

One downfall is that our views files are not created but this is a fairly quick task in comparison.
Generators are very helpful tools and boost work efficiency a lot. However, using them carelessly can end in a mess with multiple overlapping files, migrations out of sync and our RESTful routes simplified to only GET’s. Here’s some things to remember when working with them:
⏦ Controller methods must still be edited!
⏦ db:migrate and db:seed!
⏦ How to undo your changes… ‘rails destroy thing_you_just_created’
Finally, here is a list of all other generators that rails offers, if you found this post interesting or helpful, please go and look at these further:
assets
generators
helper
inherited_resources_controller
integration_test
mailer
observer
performance_test
responders_controller
scaffold
scaffold_controller
session_migration
task
