Rails Resource vs Rails Scaffold

Jami Sheumaker
2 min readOct 13, 2021

When starting a new rails backend you need to start with building several different files out. One way to make this easier is to generate the files using “rails generator”. Obviously you could just generate files as you go, or you can can generate several at once using “rails g resource” or “rails g scaffold”. Here’s a breakdown of the two to see the benefits and downsides of each.

Rails G Resource

Rails g resource generates the following:

  • A migration file that will create a new database table for the attributes passed to it in the generator
  • A model file that inherits from ApplicationRecord
  • A controller file that inherits from ApplicationController
  • A view directory, but no view template files
  • A view helper file
  • A scss file for the styles for the controller
  • A full resources call in the routes.rb file

Rails G Scaffold

Rails g scaffold generates the following:

  • A migration file that will create a new database table for the attributes passed to it in the generator
  • A model file that inherits from ApplicationRecord
  • A controller file that inherits from ApplicationController
  • View templates for each of the controller actions that render a view
  • The full set of RESTful routes
  • And every other component needed for a functional CRUD environment

The differences

Looking at the two options they clearly have a lot of similarities. This means they have a lot of the same benefits. The biggest differences have to do with the controller actions and routes. While scaffold building out these baseline controller actions can be helpful, it can also cause some issues. The methods it builds are very specific and don't always follow along with your needs, which means you will more than likely want to delete them completely or slightly rewrite them. This can make it more confusing than just writing your methods from scratch. Also as a developer we generally want to make our code as clean and short as possible and when using scaffold this can create a lot of extra code you don't even need. In the end, jut know what is needed for your project. If you need full CRUD for something, use scaffold. If not you probably could just just use resource and get most of what you need without all the extra unnecessary code.

--

--