0

I previously had this question: Rails Amounts in Thousands Are Truncated

However, I've run into the issue again with numbers that are greater than 1000, but don't have a comma.

So, for example, when "1590" is read in from a JSON response, my DB stores 1.00 in the DB for reg_price.

[
  {
    "reg_price": "1590"
  }
]

Schema

 create_table "products", force: :cascade do |t|
   t.decimal  "reg_price",                           precision: 10, scale: 2
 end

Model

response = open_url(url_string).to_s
products = JSON.parse(response)

products.each do |product|
  product = Product.new(
    reg_price: item['reg_price'].gsub(',', ''),
  )
  product.save
end

Thank you!

yellowreign
  • 3,528
  • 8
  • 43
  • 80
  • I follow storing currency value at lowest measurement unit, like paisa or cents. While presenting the required format is handled via money gem – Nithin Sep 26 '17 at 06:44
  • 1
    `reg_price: item['reg_price'].gsub(',', '')` There is something wrong here. Where did `item` come from? – chumakoff Sep 26 '17 at 07:54
  • 1
    Does `Product.new(reg_price: "1590").save` result in a DB value of `1.00`? – Stefan Sep 26 '17 at 09:30

0 Answers0