ActiveRecord provides drop_table
method to drop a table from database using migration.
class DropPosts < ActiveRecord::Migration[7.2]
def change
drop_table :posts
end
end
Before
If we want to drop multiple tables in one call using drop_table
, ActiveRecord did not provide direct support.
So we have to either call drop_table
multiple times or use raw SQL.
class DropMultipleTables < ActiveRecord::Migration[7.2]
def change
drop_table :users
drop_table :posts
drop_table :comments
end
class DropMultipleTables < ActiveRecord::Migration[7.2]
def change
[:users, :posts, :comments].each do |table_name|
drop_table table_name
end
end
end
ActiveRecord::Base.connection.execute 'DROP TABLE users, posts, comments'
After
Rails 8 adds support to drop multiple tables in one call using drop_table on ActiveRecord.
drop_table :table1, :table2, :table3
With this update, Rails allows drop_table
to accept an array of table names.
class DropMultipleTables < ActiveRecord::Migration[8.0]
def change
drop_table :users, :posts, :comments
end