Created
March 3, 2016 19:09
-
-
Save myers/fc55aff9b05ca974b210 to your computer and use it in GitHub Desktop.
How to dump the data in a Rails model 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
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