Rails Model Generator

Luis Penaloza
3 min readNov 17, 2020

Generating models and controllers in rails are the best thing you can do, as not only does it save you time on making all the files, but it also helps you from making mistakes and later trying to figure out what file you created wrong. These tools are already inside rails to help coders make files and make it overall easier and more efficient for us to create. I will explain the steps on creating these generators as well as implementing an example that I have created, so you can apply them with your own code as well.

The first thing we have to do in order to create a generator for models will be to go in the terminal and use this code below (this is just an example).

With this line of code, Rails knows that we want an Article model. With that model, we want it to have a title attribute of type string, text attribute of type of text, and a page’s attribute of integer. This will also create four different folders. Two of them are the most important which are the migration and the models file. After you have completed that, you want to run rails db:migrate, and rails will execute this migration command and tell you it created the Articles table.

It will look something like the code above. There are other generators in rails that help you create folders and test to make your application run, and it’s a great tool that Ruby has implemented into its code. You can check out the different generators by putting rails generator or rails g just to be quicker and check out all the generators rails has to offer.

--

--