Skip to content

Instantly share code, notes, and snippets.

@jedprentice
Created April 13, 2022 15:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedprentice/676917aa7e08dc1168227e0eebca383d to your computer and use it in GitHub Desktop.
Save jedprentice/676917aa7e08dc1168227e0eebca383d to your computer and use it in GitHub Desktop.
Exports Active Record objects to CSV
# Copy to Rails console and call with a class parameter. Assumes
# /var/tmp exists and is writable
require 'csv'
def export_objects(klass)
objects = klass.last(10000)
CSV.open("/var/tmp/#{klass}.csv", 'w') do |row|
row << klass.attribute_names
objects.each do |o|
row << o.attributes.values
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment