Created
July 2, 2012 17:58
-
-
Save owainlewis/3034611 to your computer and use it in GitHub Desktop.
Export 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
# Export Active Record model as a CSV file | |
# | |
def self.render_csv active_record_model | |
CSV.generate do |csv| | |
csv << active_record_model.column_names | |
active_record_model.all.each do |m| | |
values = active_record_model.column_names.map{ |f| m.send f.to_sym } | |
csv << values | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment