title: Roo with rails4 giving undefined method `[]' for nil:NilClass
body: <p>I'm trying to import CSV and Excel files into a rails 4 project (with validation) using the Roo gem, based on <a href="http://railscasts.com/episodes/396-importing-csv-and-excel" rel="nofollow">http://railscasts.com/episodes/396-importing-csv-and-excel</a>.</p>
  
  <p>I've made some changes to account for Rails4 instead of Rails3 and for changes to Roo, and my ProjectImporter model now looks like:</p>
  
  <pre><code>class ProductImport
    include ActiveModel::Model
    attr_accessor :file
  
    def initialize(attributes = {})
      attributes.each { |name, value| send("#{name}=", value) }
    end
  
    def persisted?
      false
    end
  
    def save
      if imported_products.map(&amp;:valid?).all?
        imported_products.each(&amp;:save!)
        true
      else
        imported_products.each_with_index do |product, index|
          product.errors.full_messages.each do |message|
            errors.add :base, "Row #{index + 2}: #{message}"
          end
        end
        false
      end
    end
  
    def imported_products
      @imported_products ||= load_imported_products
    end
  
    def load_imported_products
      spreadsheet = open_spreadsheet
      spreadsheet.default_sheet = spreadsheet.sheets.first
      puts "!!! Spreadsheet: #{spreadsheet}"
      header = spreadsheet.row(1)
      (2..spreadsheet.last_row).map do |i|
        row = Hash[[header, spreadsheet.row(i)].transpose]
        product = Product.find_by(id: row['id']) || Product.new
        product.attributes = row.to_hash.slice(*['name', 'released_on', 'price'])
        product
      end
    end
  
    def open_spreadsheet
      case File.extname(file.original_filename)
        when ".csv" then
          Roo::CSV.new(file.path, nil)
        when '.tsv' then
          Roo::CSV.new(file.path, csv_options: { col_sep: "\t" })
        when '.xls' then
          Roo::Excel.new(file.path, nil, :ignore)
        when '.xlsx' then
          Roo::Excelx.new(file.path, nil, :ignore)
        when '.ods' then
          Roo::OpenOffice.new(file.path, nil, :ignore)
        else
          raise "Unknown file type #{file.original_filename}"
      end
    end
  end
  </code></pre>
  
  <p>When I try to run an import (using test CSV data), it fails on <code>header = spreadsheet.row(1)</code> with the error <code>undefined method '[]' for nil:NilClass</code>. The extra <code>puts</code> statement I've included confirms that <code>spreadsheet</code> itself isn't nil: it gives <code>!!! Spreadsheet: #&lt;Roo::CSV:0x44c2c98&gt;</code>. But if I try to call almost any of the expected methods on it, such as <code>#last_row</code>, it gives me the same undefined method error.</p>
  
  <p>So what am I doing wrong?</p>