I need to export everything from one table into a CSV file, I've used the faster CSV gem before, but it stopped working with newer versions of rails. Does anyone have another way i could use?
Ryan Bates has a handy railscast on exactly this topic: http://railscasts.com/episodes/362-exporting-csv-and-excel
To convert an active record database to csv just from the console (w/o a controller or view) directly to a file would 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(""))}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With