Skip to content

Instantly share code, notes, and snippets.

@vin0010
Forked from foxumon/csv_export.rb
Created August 25, 2019 16:27
Show Gist options
  • Save vin0010/06d0d76713baa677a20bd2442a95eda8 to your computer and use it in GitHub Desktop.
Save vin0010/06d0d76713baa677a20bd2442a95eda8 to your computer and use it in GitHub Desktop.
Export table to CSV with all attributes in rails console
require 'csv'
file = "#{Rails.root}/public/data.csv"
table = User.all;0 # ";0" stops output. Change "User" to any model.
CSV.open( file, 'w' ) do |writer|
writer << table.first.attributes.map { |a,v| a }
table.each do |s|
writer << s.attributes.map { |a,v| v }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment