Optimize Your Mod 2 Code Challenge

Robert Vidal
4 min readMar 18, 2019

With the Mod 2 code challenge fast approaching, we have the daunting task of meeting the requirements in the short time frame of 75 minutes. As an error prone new developer, I worry about being able to finish everything I need to in the allotted time. This blog will demonstrate the time I saved by using the rails generate model method and rails generate resource method vs. doing everything by hand. We can drastically reduce our time spent on tedious work if we use these tools, allowing us to focus on the code.

Starting any rails project from scratch will first involve you making sure you are in your desired folder, from there run rails new sample_name in the terminal. This will create the folders we have all become accustomed to in the labs and pairings. After cd-ing into your new project, you are now able to either set up the MVC structure by hand, with the rails generate model method, or with the rails generate resource method. That being said, it’s my opinion that the last method is the most useful and least likely to cause error in your code.

Doing everything by hand = 5 minutes after checking on inheritance/syntax

  • Create everything by hand (routes, migrations, views folder, model, controller).
  • More susceptible to errors/typos.
  • Inherit ActiveRecord and ApplicationController yourself.

Rails g model method = 4 minutes, 2 seconds.

Terminal input with Try_1 representing the model name.
  • Generates a model(+)
  • Generates a migration with data type(s) and column name(s)(+)
  • You still need to create a Views folder(-)
  • You still need to create a controller(-)
  • You need to write your routes(-)

This method will allow you to create your models, their columns/data types in the migration. Although, it is limited to these two features, meaning we will then have to build the corresponding views folder, controller file, and write the routes for the controller.

The model and migration created with this method.

Rails g resource method = 2 minutes, 16 seconds

The new terminal syntax, changing out “model” for “resource”.
  • Generates a model(+)
  • Generates a migration with data type(s) and column name(s)(+)
  • Generates a Views folder for the specific model(+)
  • Generates a controller and inherits from the ApplicationController(+)
  • Writes your routes for you(+)

In my opinion, the rails g resource method is one of the keys to succeeding in the code challenge this Friday. With this tool we can auto generate things like the controller (with inheritance to the ApplicationController), the model (with inheritance to ActiveRecord), a completed migration, and a designated Views folder. The only thing we will have to do when using this method is create the controller methods and appropriate .erb files to match.

Conclusion:

While the difference in time may be just 2 to 3 minutes if you compare doing everything by hand to using the rails g resource method, that time difference could become much greater should you run into any syntax errors or set up your migrations improperly. Using either of the two methods provided can save you a great deal of time and stress as it does much of the heavy lifting for you. And if this isn’t enough for you, take a look at scaffolding in rails after you pass your code challenge!

Courtesy of AREKEEOO on imgur

--

--

Robert Vidal

Full stack software engineer looking to expand my understanding of all things code! UH ‘18 and Flatiron ’19 alum.