5

I created 2 models and ran the migrations, attempted some work on each of them and now I would like to start over and approach them differently. I'm new to Rails and have never attempted to delete/remove database tables (apart from rolling them back right after I migrated them).

Thanks!

teecraft
  • 927
  • 2
  • 9
  • 10
  • See the top answer to the question [Rails - How to ReCreate the database][1]. [1]: http://stackoverflow.com/questions/4116067/rails-how-to-recreate-the-database – Teemu Leisti Feb 28 '12 at 05:21

4 Answers4

6

To drop a table during a migration, you can rails g migration DropUsers

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

  def down
    # recreate table logic here
  end
end

You can also drop tables from the Rails console

ActiveRecord::Migration.drop_table(:users)

FYI If you want to remove scaffold-created code, rails destroy scaffold User

scarver2
  • 7,887
  • 2
  • 53
  • 61
5

Create another migration and in self.up

drop_table :tablename

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-drop_table

David Lyod
  • 1,438
  • 12
  • 14
1

If you never committed the changes or deployed the app, you can simply remove the model files and remove the tables from your database using the database console (assuming it's sqlite3 type sqlite3 to enter the shell console) or a database administration GUI.

Otherwise, you will need to use the drop_table migration to reflect the changes on the production system.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • Thanks. I was wondering if I could use SQLite DB Browser to just delete the tables. Can I just delete old migration files as well? – teecraft Oct 12 '10 at 14:00
0

See the top answer to the question Rails - How to ReCreate the database

Community
  • 1
  • 1
Teemu Leisti
  • 3,750
  • 2
  • 30
  • 39