Skip to content

Instantly share code, notes, and snippets.

@myers
Created March 3, 2016 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myers/fc55aff9b05ca974b210 to your computer and use it in GitHub Desktop.
Save myers/fc55aff9b05ca974b210 to your computer and use it in GitHub Desktop.
How to dump the data in a Rails model to csv
model = MyModel
CSV.new(File.new(model.to_s.underscore + ".csv", "w")).tap do |csv|
column_names = model.column_names
csv << column_names
model.find_each do |ii|
csv << column_names.map do |cn|
ii.send(cn)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment