If you have a Rails project and want to export a table as a CSV, without having to go to all the trouble of finding a gem, installing and using it, then uninstalling it when it’s no longer needed, I have some good news. Here’s an easy and quick way to export a particular table from your database as a CSV file.
This is the code that you need to run. You can put it as a rake task and run it, or run it another way.
As you can see, first we import
CSV— we need it to do the writing of the CSV file with the data from the database. We then choose the location and the name for the file that we want to export it to, which in our case will be a file called data.csv included under repository public.
Then we set the table that we want to export and start writing. We could also change the attributes that we want to export — we don’t have to include them all as they are in the database.
That’s it! It’s that simple and yet very helpful.
This article was originally published on Medium