Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails remove old models with migrations

I have a bunch of rails models that i'm re-writing into a single model to simplify my code and reduce unnecessary tables.

I'm wondering what the best way to delete a model class and its table is. I want past migrations to still succeed, but I don't want to leave the empty models lying around. Do I have to manually delete the old migrations that reference these models, then manually delete the class files?

Does anyone have any tips for the best way to do this?

like image 453
brad Avatar asked Dec 30 '09 18:12

brad


2 Answers

All in one solution.

Run the following commands:

rails destroy model ModelName
rails g migration DropTableModelName

The former will generate a new migration file which should looks like this:

class DropTableModelName < ActiveRecord::Migration
  def change
    drop_table :model_name
  end
end

Now run db:migrate and you're done.

like image 78
Lucio Avatar answered Sep 27 '22 19:09

Lucio


If you'd like to completely get rid of of a model and its table do this:

rails destroy model Name
like image 28
djblue2009 Avatar answered Sep 27 '22 20:09

djblue2009