Answer the question
In order to leave comments, you need to log in
How to properly import csv file data into Rails without headers?
I import with this not-too-elegant code in the model:
def self.import(file)
CSV.foreach(file.path, :encoding => 'bom|utf-8', :quote_char => "|", headers: true, :col_sep => "\t") do |row|
product = find_by_ones(row["ones"]) || new
xxx = row['price'].sub(" ", "").sub(" ", "").sub(" ", "").sub(" ", "")
h2 = {"price" => xxx}
product.attributes = row.to_hash.except("title").merge(h2)
product.save!
end
end
Answer the question
In order to leave comments, you need to log in
Decision:
CSV.foreach(file.path, :encoding => 'bom|utf-8', :quote_char => "|", :col_sep => "\t", :headers=>:first_row) do |row|
product_hash = {"ones" => row[0], "title" => row[1], "price" => row[2]}
product = find_by_ones(product_hash["ones"]) || new
xxx = product_hash['price'].sub(" ", "").sub(" ", "").sub(" ", "").sub(" ", "")
h2 = {"price" => xxx}
product.update_attributes(product_hash.except("title").merge(h2))
product.save!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question