Export to CSV in Rails

I need to export everything from one table to a CSV file, I used to use a faster CSV stone, but it stopped working with newer versions of rails. Does anyone have another way that I could use?

+4
source share
3 answers

Ryan Bates has a convenient rail program on this topic: http://railscasts.com/episodes/362-exporting-csv-and-excel

+2
source

For converting the active database of records in csv only from the console (without a controller or view) directly to the file, there will be something like this

tags = [Model.column_names] rows = tags + Model.all.map(&:attributes).map(&:to_a).map { |m| m.inject([]) { |data, pair| data << pair.last } } File.open("ss.csv", "w") {|f| f.write(rows.inject([]) { |csv, row| csv << CSV.generate_line(row) }.join(""))} 
+1
source

Check the CSV module. I did what you are trying to do with this article, http://www.funonrails.com/2012/01/csv-file-importexport-in-rails-3.html .

-1
source

All Articles