Skip to content

Instantly share code, notes, and snippets.

@bradley
Created October 3, 2018 16:39
Show Gist options
  • Save bradley/93c0c1cfba2353f80fa1eed2dc2f52c7 to your computer and use it in GitHub Desktop.
Save bradley/93c0c1cfba2353f80fa1eed2dc2f52c7 to your computer and use it in GitHub Desktop.
Export a CSV on a production Rails Server
# When ran in the rails console, this code will create and place a
# new csv file in the tmp directory of your rails app. Once done,
# simply exit the rails console and cd into the tmp directory, cat
# the file, and copy its content to a local file. There are probably
# better ways to do this but this does work. Be sure to clean up
# after yourself by rm'ing the file when youre done with it.
require 'csv'
zoo = Zoo.find(id)
animals = zoo.animals
CSV.open("tmp/#{zoo.name}-animals-export.csv", "wb") do |csv|
csv << ["Class", "Genus", "Name", "Age"]
animals.each do |animal|
csv << [
animal.animal_class.name,
animal.animal_genus.name,
animal.name,
animal.age,
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment