Created
April 13, 2022 15:12
-
-
Save jedprentice/676917aa7e08dc1168227e0eebca383d to your computer and use it in GitHub Desktop.
Exports Active Record objects to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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