What is the best way to drop a table & remove a model in Rails 3?
Solution 1:
The second method is the ideal way to handle this: your migration files are meant to represent how your database has changed over time. The older migration files will remain in your project (in case, hypothetically, you wanted to roll back to an older version), but Rails will not run them when you rake db:migrate
because it knows they've already been run (based on data in the database's schema_migrations table).
Your schema.rb will just be updated once to reflect that your database no longer contains that table.
One minor tweak to your code: your migration file should drop the table in the up
method, and ideally recreate it in the down
method. The "up" signifies that your migration drops the table to move forward in time, and if the migration is rolled back, the down
method will be run.
Solution 2:
I know this is an old thread. More often than not, you want to remove not only the model, but routes, controller and views associated with that model as well. To do that, run these
rails g migration DropYourModel
rails destroy scaffold YourModelName
Edit your migration file to drop_table
and then run
rake db:migrate
If the model happens to be defined in a namespace, e.g., admins
, replace the first command with
rails destroy scaffold admins/YourModelName