What is the best way to drop a table & remove a model in Rails 3?

风格不统一 提交于 2019-11-28 16:10:59

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.

MichaelZ

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!