All Your Records As CSV, fast and generic

Our admins are always data-greedy, so i provided a method that lets them get detailed data on any search result they like. Be careful to not let normal users access this (hide email addresses etc).


  • Provide a current_model method that returns which model the controller is working on.
  • Store the objects you want to render in @current_objects.
  • add this block to every index action response (in a DRY way…)


  • Visit /xxx/anything.csv to get all data in csv
format.csv do
  #collect data
  keys =
  csv_string = FasterCSV.generate do |csv|
    csv << keys
    @current_objects.each do |record|
      csv << record.attributes.sort_by{|k,v|k}.map{|arr|arr[1]}

  #send data
  filename = current_model.to_s.downcase.gsub(/[^0-9a-z]/, "_") + ".csv"
    :type => 'text/csv; charset=utf-8; header=present',
    :filename => filename

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s