Skip to content

Instantly share code, notes, and snippets.

@owainlewis
Created July 2, 2012 17:58
Show Gist options
  • Save owainlewis/3034611 to your computer and use it in GitHub Desktop.
Save owainlewis/3034611 to your computer and use it in GitHub Desktop.
Export Rails model to CSV
# 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