Friday, 10 April 2015

clear all tables data in one command in ruby on rails

In lib/tasks/dataclear.rake

namespace :dataclear do
  desc "Truncate all tables"
  task :truncate => :environment do
    conn = ActiveRecord::Base.connection
    tables = conn.execute("show tables").map { |r| r[0] }
    tables.delete "schema_migrations"
    tables.each { |t| conn.execute("TRUNCATE #{t}") }
  end
end

Then run "rake dataclear:truncate"

No comments:

Post a Comment