Skip to content

Instantly share code, notes, and snippets.

@roberthopman
Created May 9, 2020 19:05
Show Gist options
  • Save roberthopman/9a6a380844b18e4ebe3f4e193b5336a0 to your computer and use it in GitHub Desktop.
Save roberthopman/9a6a380844b18e4ebe3f4e193b5336a0 to your computer and use it in GitHub Desktop.
require 'csv'
namespace :csv do
desc "generates a migration file from column names from the CSV headers"
task create_migration: :environment do
def headers_from_CSV (filename)
csv_header_array = CSV.read(filename, headers: true).headers
return csv_header_array.map { |col_a| col_a.strip.downcase.gsub(' ', '_').gsub('.', '_') }
end
file = Rails.root.join('db', 'excel_seeds.csv')
new_col_command = headers_from_CSV(file).join(' ')
if (new_col_command.length != 0)
sh "rails generate migration AddAttributesToPlaces #{new_col_command}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment