title: How to use CSV Writer in rake task?
body: <p>I am writing a rake task to output data from database and upload to ftp server.</p>
  
  <p>The following seems to work:</p>
  
  <pre><code>require 'csv'
  
  task export do
    CSV.open("temp_files/message_data.dat", "wb", '|') do |csv|
      csv &lt;&lt; ['foo', 'bar', baz']      
    end
  end
  </code></pre>
  
  <p>Now, to access a model, I seem to need </p>
  
  <pre><code>task :export =&gt; :environment do
  </code></pre>
  
  <p>This causes the code to fail: </p>
  
  <pre><code>rake aborted!
  can't convert String into Integer
  </code></pre>
  
  <p>--trace shows:</p>
  
  <pre><code>/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'
  </code></pre>
  
  <p>How to use CSV Writer from a task and access a model?</p>
  
  <p>Working in Ruby 1.9.2, Rails 3.0.7. </p>
  
  <p>Thanks.</p>