November 14, 2011
Export CSV with Mongoid and FasterCSV
In my research I found a hundred and more different ways to manage the task of exporting data into a CSV file. Whenever I’m learning something new, on a new system or with a new language, I like to build the process myself from scratch. And being a minimalist programmer it just seems fitting. Enjoy

def export_inverts require 'fastercsv' inverts = Invert.all filename = params[:action] + ".csv"
#this is required if you want this to work with IE if request.env['HTTP_USER_AGENT'] =~ /msie/i headers['Pragma'] = 'public' headers["Content-type"] = "text/plain" headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0' headers['Content-Disposition'] = "attachment; filename=\"#{filename}\"" headers['Expires'] = "0" else headers["Content-Type"] ||= 'text/csv' headers["Content-Disposition"] = "attachment; filename=\"#{filename}\"" headers["Content-Transfer-Encoding"] = "binary" end
csv_string = FasterCSV.generate do |csv| csv << ["Genus","Species","Common Name","Pet Name","Gender"] inverts.each do |i| csv << [i.scientific_name,i.scientific_name,i.common_name,i.pet_name,i.gender] end end render :text => csv_stringendNOTE: Developers using Ruby 1.8 <= must install the fastercsv gem.
2 Comments on “Export CSV with Mongoid and FasterCSV”
Leave a Comment
JP Silvashy
January 12, 2012What would your route for this action look like?
Michael Minter
January 13, 2012Something simple like:
get “export_inverts” => “exims#export_inverts”, :as => “export_inverts”
Could go in your routes file to handle this action.