2016-01-12 14 views
1

正如this documentation所述,我可以更改標準col_sep例如從到 」;」。Rails:使用多於1個col_sep

我該如何改變它以同時使用「,」和「;」作爲col_sep。

我的實際代碼:

def process! 
    @imported_count = 0 
    CSV.foreach(file.path, headers: true, header_converters: :symbol, col_sep: ",") do |row| 
    order = Order.assign_from_row(row) 
    if order.save 
     @imported_count += 1 
    else 
     errors.add :base, "Line #{$.} - #{order.errors.full_messages.join(",")}" 
    end 
    end 
end 

先謝謝了!

+0

解決方法在這裏找到:http://stackoverflow.com/a/7832423/5243191 – CottonEyeJoe

回答

1

col_sep只接受一個值。你可以看到它是如何在這裏使用的例子:

http://rxr.whitequark.org/mri/source/lib/csv.rb (線1654和1803是幾個例子)

一個解決辦法可以通過使用類似GSUB被替換爲另一個分離值的所有實例。不是你希望的銀彈,但根據你的要求它可以做到這一點!

+0

解決方法在這裏找到:http://stackoverflow.com/a/7832423/5243191 – CottonEyeJoe