Skip to content

Instantly share code, notes, and snippets.

@igorb
Forked from foxumon/csv_export.rb
Created December 6, 2018 12:48
Show Gist options
  • Save igorb/2f5708a65d53625c97bd5d6ecc7fde37 to your computer and use it in GitHub Desktop.
Save igorb/2f5708a65d53625c97bd5d6ecc7fde37 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
@igorb
Copy link
Author

igorb commented Jan 15, 2019

CSV.open( file, 'w' ) do |writer|
writer << table.first.attributes.map { |a,v| a }
table.each do |s|
attr = s.attributes.map do |a,v|
v = Firm::FIRM_TYPES[v].to_s if a == 'firm_type' and !v.nil?
v
end
writer << attr
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment