task :generate_csv_from_db => :environment do |_task, args| def generate_csv(model_name) model = model_name.constantize results = model.pluck(*model.column_names) return 0 if results.size.zero? data = CSV.generate do |csv| csv << model.column_names results.each { |result| csv << result } end file_path = "db/fixtures/bulk_insert/#{model_name}.csv" File.open(file_path, 'w') do |file| file.write(data) p "Finished generating #{file_path}(#{results.size} rows) from DB!” end return results.size end total_count = 0 ActiveRecord::Base.connection .tables .reject{ |t| t.in? %w(schema_migrations ar_internal_metadata) } .each { |t| total_count += generate_csv(t.classify) } p "Total row count: #{total_count}" end end CSVʹશϨίʔυग़ྗ id,title,author,created_at,updated_at 1,ֶͷ͢͢Ί,་٢,2021/02/01 12:33,2021/02/01 12:33 2,ҏ౾ͷགྷΓࢠ,߁,2021/02/01 12:33,2021/02/01 12:33 …… ←