views:

29

answers:

1

Ok so I am trying to import a csv which I need to insert into a db through rails or anyway that would be quickest so i decided to create a dummy rails application to do the importing. Here is some of the code below which I got some of it from a blog I was reading.

def upload
  table = ImportTable.new :original_path => params[:upload][:csv].original_path
  row_index = 0    
  FasterCSV.parse(params[:upload][:csv]) do |cells|

  column_index = 0
  cells.each do |cell|
    table.import_cells.build :column_index => column_index, :row_index => row_index, :contents => cell
    column_index += 1
  end
  row_index += 1
end
  table.save
  redirect_to import_table_path(table)
end

All works well but the format of the csv is not in a way that is making this process easy. Here is the format and what i need to happen.

NELCO 2GB RAM, 250GB HDD, DVD-RW, WIN7 PRO / XP LOADED  $575.00     1   $575.00     2   $1,150.00   3   $1,725.00   4   $2,300.00   5   $2,875.00 
TOUCHSCREEN, 15" LCD, SPKRS     $489.00     1   $489.00     2   $978.00     3   $1,467.00   4   $1,956.00   5   $2,445.00 
THERMAL RECEIPT PRINTER FRONT LOADING   $324.00     1   $324.00     2   $648.00     3   $972.00     4   $1,296.00   5   $1,620.00 
IMPACT PRINTER, SERIAL  $285.00     1   $285.00     1   $285.00     1   $285.00     1   $285.00     1   $285.00 
CASH DRAWER, 16x16  $127.00     1   $127.00     1   $127.00     1   $127.00     2   $254.00     2   $254.00 
RESTAURANTS PRO LICENSE $699.00     1   $699.00     2   $1,398.00   2   $1,398.00   2   $1,398.00   2   $1,398.00 
RESTAURANTS PRO LICENSE FOR ADDITIONAL STATIONS BEYOND 2    $535.00         $0.00       $0.00   1   $535.00     2   $1,070.00   3   $1,605.00 
CREDIT CARD PROCESSING SOFTWARE + CARD READER   $79.00  1   $79.00  2   $158.00     3   $237.00     4   $316.00     5   $395.00 
FIREWALL & STATEFUL PACKET INSPECTION   $89.00      $0.00   1   $89.00  1   $89.00  1   $89.00  1   $89.00 
STANDARD SUPPORT PLAN FIRST STATION $275.00     1   $275.00     1   $275.00     1   $275.00     1   $275.00     1   $275.00 
SUPPORT PLAN ADDT'L STATIONS    $100.00         $0.00   1   $100.00     2   $200.00     3   $300.00     4   $400.00 
PREPAID TRAINING & SUPPORT BUNDLE (1 HR.)   $89.00  1   $89.00  1   $89.00  1   $89.00  1   $89.00  1   $89.00 

Sorry if its hard to read i did my best at formatting. The first element in each row is the name of the product so i need that stored in Product.name and the second is the price so that will go in Product.price. the next one is where i am having problems

in the loop

      cells.each do |cell|

I need to check to see if I am at the third element in the row and do something and the fifth element and do another thing but i tried cell[2] and got something funky like this

    Rails.logger.info "====3rd postion==========#{cell[3]}"
  =>    ====3rd postion===========79
+1  A: 

Are you intending to actually do cells[3] instead of cell[3]? cell is a string and not an array. Depending on which version of ruby you're using, cell[3] will either be an integer representing the ascii character, or the 2nd character in the string.

http://ruby.about.com/od/newinruby191/qt/stringindexop.htm

Beerlington