docwhat's avatardocwhat's blog

Undoing a rails generate

I just learned this today, while taking a Ruby on Rails class from Jumpstart Lab.

I have, many times, messed up a rails generate command. For example, using singular where plural is needed or the reverse.

Today, I learned that you can undo a generate by using rails destroy with the same arguments you used for the generate command.

Example:

rails generate controller article
# Oops!
rails destroy controller article
rails generate controller articles

Apparently only a few of us in class knew about this during this class.

I’d worked around not knowing this by using git to commit everything and then reverting everything if I messed up.

Why doesn’t a generate command show a message explaining that it can be undone with destroy? It’d be really really helpful.

Ciao!

PS: I also discovered you can “redo” the last migration as well, which is really handy as well for repairing mistakes:

rake db:migrate:redo

Comments

Gravatar for sam
Sam

Im just starting and this is the first question I asked! Thank You!

Gravatar for jean brick
Jean Brick

I was doing a video with a instructor and I misspelled the name for the generator, this allowed me to delete it so I could spell it right. Thank you a ton, I just hit this problem and found this solution within 60 seconds.

Submit a Comment

docwhat

The personal blog of Christian Höltje.
docwhat docwhat contact