Removing a model in rails (reverse of "rails g model Title...")
Asked Answered
I

6

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

How can I completely remove this model? Thanks

Injection answered 18/8, 2011 at 9:41 Comment(1)
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.Palma
V
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>
Venose answered 18/8, 2011 at 10:35 Comment(5)
:( Read the first line and did it. It was my fault, but a correctly-ordered answer may have helped.Cenis
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.Venose
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 suggestCenis
@MikeT You what? Restored the migration from the trash? You don't use any VCS? o_O You absolutely should!Stoner
Worked for me, but I also had to go into routes.rb and delete " devise_for :MODEL"Irritate
Z
65

Try this

rails destroy model Rating

It will remove model, migration, tests and fixtures

Zacharia answered 18/8, 2011 at 9:43 Comment(1)
It removes all, but it do not create downgrade migration or something.Tinctorial
A
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
Alric answered 6/9, 2013 at 18:5 Comment(1)
Great answer, will behave as expected with a capistrano deploy.Trinatte
G
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
    
Garrulous answered 29/8, 2016 at 5:18 Comment(1)
rails d model is what I'm looking for to reverse my rails g model.Servetnick
O
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.

Outpoint answered 9/9, 2018 at 22:19 Comment(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.Realgar
P
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
Pyrophoric answered 6/4 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.