简体   繁体   中英

How to use CSV Writer in rake task?

I am writing a rake task to output data from database and upload to ftp server.

The following seems to work:

require 'csv'

task export do
  CSV.open("temp_files/message_data.dat", "wb", '|') do |csv|
    csv << ['foo', 'bar', baz']      
  end
end

Now, to access a model, I seem to need

task :export => :environment do

This causes the code to fail:

rake aborted!
can't convert String into Integer

--trace shows:

/home/baller/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/csv.rb:1335:in `initialize'
/home/baller/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/csv.rb:1335:in `open'

How to use CSV Writer from a task and access a model?

Working in Ruby 1.9.2, Rails 3.0.7.

Thanks.

OK, I got it working. The call to CSV.open has changed:

CSV.open("temp_files/message_data.dat", "wb", :col_sep => '|')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM