title: Ruby on Rails Import CSV into MySQL
body: <p>Newbie question....</p>
  
  <p>Trying to start a project in rails. I have different spreadsheets in csv format I'd like to import into the MySQL database to be able to manipulate the data.</p>
  
  <p>After looking around on stackoverflow, Google, etc. I wrote a rake task requiring fastercsv to do the job. I keep getting errors so hopefully you can help.</p>
  
  <p>...
  <strong>Ok so I changed the code to use 'csv' vs 'fastercsv'...still getting errors</strong>. See below</p>
  
  <p>New Code for Rake File (take 3):</p>
  
  <pre><code>require 'csv'
  
  desc "Import gac from csv file"
  task :import =&gt; [:environment] do
  
    file = "gac.csv"
  
    CSV.foreach(file, :headers =&gt; true) do |row|
      Institution.create({
      :institution_name =&gt; row[0],
      :website =&gt; row[1],    
      :email =&gt; row[2],
      :category_1 =&gt; row[3],
      :category_2 =&gt; row[4],
      :category_3 =&gt; row[5],
      :category_4 =&gt; row[6],
      :category_5 =&gt; row[7],
      :category_6 =&gt; row[8],
      :category_7 =&gt; row[9],
      :category_8 =&gt; row[10],
      :category_9 =&gt; row[11],
      :category_10 =&gt; row[12],
      :category_11 =&gt; row[13],
      :institution_description =&gt; row[14]
      })
    end
  end
  </code></pre>
  
  <p><strong>Error Codes:</strong></p>
  
  <pre><code>Daves-MacBook-Pro:vendor dave$ rake import --trace
  ** Invoke import (first_time)
  ** Invoke environment (first_time)
  ** Execute environment
  ** Execute import
  rake aborted!
  invalid byte sequence in UTF-8
  /Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1855:in `sub!'
  /Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1855:in `block in shift'
  /Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1849:in `loop'
  /Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1849:in `shift'
  /Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1791:in `each'
  /Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1208:in `block in foreach'
  /Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1354:in `open'
  /Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1207:in `foreach'
  /Users/dave/rails_projects/vendor/lib/tasks/import.rake:8:in `block in &lt;top (required)&gt;'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
  /Users/dave/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/bin/rake:33:in `&lt;top (required)&gt;'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/bin/rake:19:in `load'
  /Users/dave/.rvm/gems/ruby-1.9.3-p0/bin/rake:19:in `&lt;main&gt;'
  Tasks: TOP =&gt; import
  </code></pre>