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

How can I completely remove this model? Thanks

1
  • 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 describes exactly what to do. Mar 4, 2014 at 0:11

6 Answers 6

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>
5
  • :( Read the first line and did it. It was my fault, but a correctly-ordered answer may have helped.
    – Mike T
    Jul 22, 2013 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. Jul 22, 2013 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, 2013 at 7:03
  • 6
    @MikeT You what? Restored the migration from the trash? You don't use any VCS? o_O You absolutely should! Sep 6, 2015 at 18:06
  • Worked for me, but I also had to go into routes.rb and delete " devise_for :MODEL" Aug 28, 2018 at 18:13
65

Try this

rails destroy model Rating

It will remove model, migration, tests and fixtures

1
  • 6
    It removes all, but it do not create downgrade migration or something.
    – mr.The
    Feb 12, 2014 at 18:10
33

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
1
  • Great answer, will behave as expected with a capistrano deploy.
    – LessQuesar
    Aug 20, 2016 at 12:55
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
    
1
  • 1
    rails d model is what I'm looking for to reverse my rails g model.
    – Martin K.
    Dec 3, 2017 at 5:04
5

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.

1
  • 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. Feb 13, 2020 at 13:23
0

If you are interested in reversible migration to drop a table

class DropProductsTable < ActiveRecord::Migration[7.0]
  def change
    drop_table :products do |t|
      t.string :name
      t.text :description
      t.decimal :price, precision: 8, scale: 2
      t.timestamps
 # Rest of the stuff like indexes. Consider copying directly from schema.rb
    end
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.