Skip to content

Instantly share code, notes, and snippets.

@shah743
Created July 18, 2019 06:32
Show Gist options
  • Save shah743/384f445bcec22f3412ccbfd1b86e3019 to your computer and use it in GitHub Desktop.
Save shah743/384f445bcec22f3412ccbfd1b86e3019 to your computer and use it in GitHub Desktop.
Export From Console to CSV
require 'csv'
file = "#{Rails.root}/public/user_data.csv"
products = Product.order(:first_name)
headers = ["Product ID", "Name", "Price", "Description"]
CSV.open(file, 'w', write_headers: true, headers: headers) do |writer|
products.each do |product|
writer << [product.id, product.name, product.price, product.description]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment