Skip to content

Instantly share code, notes, and snippets.

@chand
Last active November 21, 2024 10:44
Show Gist options
  • Save chand/3c646d7ef8f32599ea17ae37c6ebde86 to your computer and use it in GitHub Desktop.
Save chand/3c646d7ef8f32599ea17ae37c6ebde86 to your computer and use it in GitHub Desktop.
Drop Table, Model, Migration in Rails

Dropping Migration Table, Models, Controller

Command Line

1. Drop Table/Migration

rails generate migration DropTablename

A file will be created, in the db > migrate folder, make sure it looks like:

class DropUsers < ActiveRecord::Migration
  def change
    drop_table :users
  end
end

2. Drop Model

rails d model user

3. Drop Controller

rails d controller users

4. Re-Migrate

be rake db:migrate
@quarkgluant
Copy link

Thanks a lot !
But be careful your migration will not be reversible.
For this, you need to add something like this :

class DropEventExport < ActiveRecord::Migration[6.0]
  def change
    drop_table :event_exports do |t|
      t.boolean "global", default: false
      t.string "file_file_name"
      t.string "file_content_type"
      t.integer "file_file_size"
      t.datetime "file_updated_at"

      t.timestamps null: false
    end
  end
end

@DemitryT
Copy link

This is great, thanks. Also, rails g migration DropTablename works as well

@aleemchishti
Copy link

Hi
what about the associations created in schema file? how to remove them

@mtpedro
Copy link

mtpedro commented Nov 5, 2023

Very helpful, thanks!

@achrafbsibiss
Copy link

how to drop table from the rails console

@Dmoment
Copy link

Dmoment commented Nov 21, 2024

how to drop table from the rails console

ActiveRecord::Base.connection.drop_table(:table_name)

@achrafbsibiss
Copy link

tanks for shearing, i find also this:
ActiveRecord::Base.connection.execute("DROP TABLE table_name")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment