129
rails g model Rating user_id:integer message:string value:integer

How can I completely remove this model? Thanks

Colbern
  • 1,291
  • 2
  • 9
  • 3
  • 6
    Mikhail's top answer below applies to a model created in the last migration. Those who want to remove a model and its table that have been around for a while, [this thread](http://stackoverflow.com/questions/15648268/what-is-the-best-way-to-drop-a-table-remove-a-model-in-rails-3) describes exactly what to do. – Ninjakannon Mar 04 '14 at 00:11

5 Answers5

214

When you generate a model, it creates a database migration. If you run 'destroy' on that model, it will delete the migration file, but not the database table. So before run

bundle exec rails db:rollback
rails destroy model <model_name>

For rails versions before 5.0 and higher use rake instead of rails

bundle exec rake db:rollback   
rails destroy model <model_name>
Mikhail Nikalyukin
  • 11,867
  • 1
  • 46
  • 70
  • :( Read the first line and did it. It was my fault, but a correctly-ordered answer may have helped. – Mike T Jul 22 '13 at 19:40
  • 4
    if it's development and you don't afraid to lose your data you can run `bundle exec rake db:drop db:create db:migrate`. It will be a new empty database. – Mikhail Nikalyukin Jul 22 '13 at 20:30
  • Ye thanks. I restored the migration from the Trash but it turns out I had another missing migration too so I'm going to do what you suggest – Mike T Jul 23 '13 at 07:03
  • 6
    @MikeT You what? Restored the migration from the trash? You don't use any VCS? o_O You absolutely should! – plunntic iam Sep 06 '15 at 18:06
  • Worked for me, but I also had to go into routes.rb and delete " devise_for :MODEL" – cormacncheese Aug 28 '18 at 18:13
65

Try this

rails destroy model Rating

It will remove model, migration, tests and fixtures

fl00r
  • 82,987
  • 33
  • 217
  • 237
31

For future questioners: If you can't drop the tables from the console, try to create a migration that drops the tables for you. You should create a migration and then in the file note tables you want dropped like this:

class DropTables < ActiveRecord::Migration
  def up
    drop_table :table_you_dont_want
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end
end
Jenny Lang
  • 371
  • 4
  • 8
24
  1. To remove migration (if you already migrated the migration)

    rake db:migrate:down VERSION="20130417185845" #Your migration version
    
  2. To remove Model

    rails d model name  #name => Your model name
    
Govind shaw
  • 407
  • 4
  • 12
4

Here's a different implementation of Jenny Lang's answer that works for Rails 5.

First create the migration file:

bundle exec be rails g migration DropEpisodes

Then populate the migration file as follows:

class DropEpisodes < ActiveRecord::Migration[5.1]
  def change
    drop_table :episodes
  end
end

Running rails db:migrate will drop the table. If you run rails db:rollback, Rails will throw a ActiveRecord::IrreversibleMigration error.

Powers
  • 18,150
  • 10
  • 103
  • 108
  • This is a useful answer - but the command doesn't work for me. I used: bundle exec rails generate migration DropEpisodes And then I would also use model deletion code below as well. – Andrew Spode Feb 13 '20 at 13:23