JT Hopple LLC

Destroying Models

JeremyRails, Tips Digg!

Surely you use the Rails generator script to create your models! You fill in the blanks in the migration for the new model, run the migration, work on the model and its tests, and then commit to Subversion. Right? And, surely after you've worked on a project for a while you've needed to remove one of these models. You write a new migration to remove the table that maps to your stupid_model and then you...

script/destroy model stupid_model --svn

Ooops! See what just happened?

D         db/migrations/002_create_stupid_model.rb

You conveniently used the --svn switch to delete the files from subversion and it not-so-conveniently deleted the initial migration for the model. It may not be obvious at first, but the next time someone tries to rake db:migrate through your series of migration scripts from start to finish it will fail when it tries to drop a table that never existed in their database (assuming it didn't fail earlier).

This morning I needed to remove a model and rather than doing it by hand (to avoid the above problem) I thought I'd take a quick look through the generator source to see if it would be easy to add an option that told the destroy model script to skip the migration. Turns out it's already there (--skip-migration) it just isn't documented!!

script/destroy model stupid_model --skip-migration --svn

Perfect! However, it might be nice if this were the default behavior...

2 Comments

aha! that *should* be default behavior!

Yes, on destroy it should probably be the default behavior.

Post a comment