V
V
Vitaly Liber2015-06-04 16:40:26
Ruby on Rails
Vitaly Liber, 2015-06-04 16:40:26

How to pull out the price element from the row and remove all spaces between the numbers?

I'm trying to do a simple import of price from csv, but the prices in the file are in this format "4000". Rails trims zeros when stored in price:integer if there is a space before between 4 and 000. It turns out just "4".
Source:

def self.import(file)
    CSV.foreach(file.path, :quote_char => "|", headers: true, :col_sep => "\t") do |row|
      product = find_by_ones(row["ones"]) || new #ищу строку, значение ones 
      product.attributes = row.to_hash.except("title") 
      product.save!
    end
  end

How can I pull the price out of row and remove the space between 4 and 000 so that when I save, I get 4000, not 4?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
timoo, 2015-06-04
@tomcat1911

The most direct option, maybe there is a better one: "4 000".split(' ').join.to_iyou get 4000.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question