Spencer Hire
Spencer Hire

Reputation: 763

Schema file ruby on rails

I've dropped my table successfully from the database from the console, using

ActiveRecord::Migration.drop_table(:foo),

But for some reason the table data still shows in the schema file.

I would like a method to do this without having to manually remove the data, (Doesn't give a proper rails feeling to it.)

Upvotes: 1

Views: 1642

Answers (2)

Alvin S. Lee
Alvin S. Lee

Reputation: 5182

The schema file located in db/schema.rb was most likely generated the very last time you ran rake db:migrate. This is because rake db:migrate migrates your db through all of the migrations in your db/migrate folder, and then it calls the db:schema:dump task, which generates the schema file based on the current state of the database.

For your situation, after you have run ActiveRecord::Migration.drop_table(:foo) in the rails console, the current state of your database (now, no longer having the table foo) is not in sync with the schema.rb file that was generated when you last migrated.

What you want to do at this point is re-dump the schema based on the current state of the database, and you do this by running rake db:schema:dump. This will take the current state of your database (without the table foo) and generate your db/schema.rb file.

For more information on the schema file and its relationship to migrations, I would recommend looking at the Rails Guide on Active Record Migrations: Schema Dumping and You.

Upvotes: 5

Carlos Morales
Carlos Morales

Reputation: 1149

While the answers provided here work properly, I wanted something a bit more 'straightforward', I found it here: link First enter rails console:

$rails console

Then just type:

ActiveRecord::Migration.drop_table(:foo)

Where :foo is the table name. And done, worked for me!

Upvotes: 0

Related Questions