2010-08-29 100 views
1

好吧,所以我想導入一個csv,我需要通過導軌或反正最快插入到數據庫中,所以我決定創建一個虛擬導軌應用程序來執行導入。以下是我在閱讀的博客中獲得的一些代碼。導入問題的產品

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 

所有的工作都很好,但是csv的格式並沒有讓這個過程變得簡單。這是格式和我需要發生的事情。

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 

對不起,如果它很難看,我做了我最好的格式。每行中的第一個元素是產品的名稱,所以我需要將其存儲在Product.name中,第二個元素是價格,以便在Product.price中使用。下一個就是我有問題

在循環

 cells.each do |cell| 

我需要檢查,看看我在該行的第三個元素,並做一些事情,第五元素,做另一件事

但我想細胞[2],得到了時髦的東西像這樣

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

回答