How to properly use Rails generators
Rails was made with the incentive of saving time when creating web apps. It is described as being magical and basically takes care of a lot of stuff for you.
Rails being rails means there are obviously ways to reduce the time you spend creating the base of your webapp and instead actually spend time programming the rest.
In comes rails generators. They take care of setting up your migration, models and controllers. The most time saving one is “scaffolding”.
Its command may looks like this

Scaffolding takes care of setting up your migrations, models, controllers, as well as all view and CRUD actions. Amazing right?
Pro-tip: we can reduce generate to a g
Well believe it or not but if you use scaffold for every rails app you make then it will make you look like a fool.
A lot of rails app won’t require all of the files you create with scaffold. Using scaffold may make your app highly inefficient because of all the empty files and classes and methods. Might work fine for you because you know what you are doing but if someone takes over they will have no clue what is and what is not needed.
Luckily I’ve got 4 different commands for you to streamline your rails creation process without making it inefficient.
Do not use all 4. Some of them have overlapping functions. Use whichever is most appropriate for your rails app.
Generate a migration
We can generate a migration to create or edit a table.
Let’s create a migration by using the create_ tag.

Here is what it creates for us

Amazing isn’t it?
Let’s say we wanted to add a column, well it isn’t hard either.
we use the add_ tag, then what the column needs to be name and what table it is added to with to_tablename. Then we specify the datatype for that new column. And here is what it creates for us:

if we wanted to get rid of a column, it is just as easy.
And here is what it create for us:

Now we just need to run the migrations.

And done! Didn’t even need to leave the console.
Create models
This generator will create a model and its migration table.
First we rails generate it, then specify the model generator. Next we give our model a name, and our table its column with their data type.
And we get both the migration and the model.


Simple right? One command line and it creates a whole table alongside its model.
Controller Generator
Only use this one if you wish to create a static view or non-CRUD stuff.
Let’s say we want to create a controller for an administrator who needs to oversees statistics of our app’s usage, while being able to do admin stuff.
Here is the command we use
We have our controller generator tag, our controller name, and its views.
This is what our controller looks like:

And what our view folder looks like:

Amazing, with one line we managed to create a controller, with its view files.
Therefore you can see why it isn’t used for CRUD, it creates a view for all specified arguments, so our “create” and “update” would get one when they shouldn’t.
Resources Generator
If you wish to build an API or use an MVC framework or wish to create your own views, this is the generator you should use.
It is very simple, just like the controller generator except we are using the resource tag, and in fact, will be creating more files for us than a controller generator.
Here is what it does:
Create a migration table, Create a model, create a controller, create our view folder (but not our view files), and it creates the routes.




Pretty cool yeah?
Now you just have to create your view and edit your controller and you’re good to go!
Side-note:
doing all of this will create test specs for us. If you don’t need those just add this bit to the command you type:

To sum-up
-Migration Generator:
Use this for: creating or editing a migration table.
It will: create/edit a migration for you and you will just need to run a db:migrate command to finalize your migration.
-Model Generator:
Use this for: creating a model and its migration table
It will: create an empty model that inherits from ApplicationRecord and it will create a full migration table for you.
-Controller Generator:
Use this for: creating a static view or non-CRUD stuff
It will: create a controller with the methods for your views, and it will create a view folder with your view files in them.
-Resources Generator:
Use this for: building an API | a front-end MVC framework |manually create your views.
It will: create a full migration table, create an empty model that inherits from ApplicationRecord, create an empty controller, create an emtpy folder in your views.
Lastly, remember not to use all of them. Use the ones you need. Otherwise you might end up with duplicates of your models, tables, or controllers.
Hope this is going to be helpful in your journey to understanding rails.