From: Adam Boyle Date: 2007-11-01T23:06:51+09:00 Subject: Re: Extending ActiveRecord for csv export... a better way? Daniel Sheppard wrote: > require 'fastercsv' > > def to_csv(records, filename=nil, columns=nil) > filename ||= "#{records.first.class.table_name}.dat" > columns ||= records.first.class.columns.map {|x| x.name } > FasterCSV.open(filename, 'w') do |csv| > records.each do |record| > csv << columns.map {|column| record.attributes[column] } > end > end > end Daniel, thank you for your reply and effort. I tried this function out with no avail. For some reason the output file is always empty (I have confirmed the table is not empty). The file has the right number of lines, but no data. I have come to the same end with this code: #within the class inheriting ActiveRecord::Base def self.export_table_to_csv(delimiter = ",", options = nil) file = File.open("#{self.table_name}.dat", File::WRONLY|File::TRUNC|File::CREAT) rec = self.find(:all, options) for row in rec do line = columns.inject([]) { |arr, col| arr << row[col]; arr} file.puts(line.join("|")) end end I am beginning to suspect that there is a problem with the columns logic in AR as related to the AR-JDBC extension (or perhaps specifically with the driver I'm using), but that is purely speculation on my part. My Rubyism is not advanced enough to know how to debug that, but I'll look at the code for AR-JDBC and see if anything jumps out at me. The link from yermej@gmail.com looks promising for setting up JRuby DBI with JDBC. I'm going to try it out if I am unable to resolve this using AR-JDBC soon! Thank you both for all your help! -- Posted via http://www.ruby-forum.com/.