Delete everything from all tables (in Activerecord)


Put this code in seed file and run rake db:seed

ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.tables.each do |table|
# MySQL
ActiveRecord::Base.connection.execute(“TRUNCATE #{table}”)

# SQLite
# ActiveRecord::Base.connection.execute(“DELETE FROM #{table}”)
end

OR

rake db:reset
It recreates your table from migrations
OR

rake db:schema:dump
rake db:schema:load

OR

rake db:drop:all
rake db:create:all

I hope it would help.
Cheers..!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s