I need to create a tab delimited ASCII file from a table (Hits) in my DB. I can already export this table into a CSV file. What's the best way to go about doing this? Is there a way to easily create this in rails?
A:
Assume the CSV data is in "something.csv" and delimited by ","
require 'csv'
File.open("tab_seperated.txt", "w+") do |f|
f << CSV.parse(File.read("something.csv")).map{|row| row.join("\t")}.join("\n")
end
Aurril
2010-03-03 07:53:35
A:
you can do it directly from rails console (or put it to the rake task) this way:
File.open('file.txt', 'w') do |f|
f.puts Hits.all.map { |h| [h.value1, h.value2].join("\t") }.join("\n")
end
uzzz
2010-03-03 09:47:29