Andy from Webcrunch

Subscribe for email updates:

Portrait of Andy Leverenz
Andy Leverenz

July 1, 2022

Last updated November 13, 2023

Rails Quick Tips - Regenerating Resources

I have fat fingers when it comes to programming some days. I’ll often make a typo when generating a new model or a controller and fudge up a process that creates a bunch of misnamed files or database records.

In my trial and error, I discovered an easy “undo button” for this problem when generating a resource.

Say you’re generating a scaffold for a Post model

rails g scaffold Post titlee:string body:text user:references

You run the command above only to notice you misspelled the title column and come to think of it you'd prefer to use rich_text on the body column. It’s too late and you create a crap ton of files thanks to the scaffolding feature. Panic sets in…

Never fear! Much like you can generate files in a pinch, you can also delete files just as fast.

Running the following reverts the entire scaffold and effortlessly removes it from your app.

rails d scaffold Post

Then you can rerun the scaffold with ease:

rails g scaffold Post title:string body:rich_text user:references

What a timesaver!

Keep in mind, If you happen to have migrated your database already you should first run rails db:rollback so there isn’t a discrepancy in the overall database schema. Any work you may have done inside the newly generated will be lost when deleted.

Link this article
Est. reading time: 1 minutes
Stats: 785 views



Part of the Rails Quick Tips collection