0

In my Rails 6 app I have a sales database with a value column. Since this is a 2 decimal number I changed the type to

change_column :sales, :value, :decimal, precision: 10, scale: 2

This app has an API where I render a sales object to json. When I read this API with my angular object and do:

console.log(typeof this.sale.id)

I get string. And when I log the value I get:

value: "40.0"

Why does this happen? Before, when the columns was simples numeric this did not happen. I know that I can convert the variable in JS to a float but I would like to know the reason and avoid converting the variable type.

almo
  • 6,107
  • 6
  • 43
  • 86
  • 1
    Does this answer your question? [Rails JSON Serialization of Decimal adds Quotes](https://stackoverflow.com/questions/6128794/rails-json-serialization-of-decimal-adds-quotes) – Dmitry Kravtsov Jan 10 '22 at 22:28
  • Yes, it does. Since the post is quite old it would be great to know what todays' best solution is...pass them as string and convert them to decimal in my Angular app or disable converting bing decimals to string in Rails. Or something totally diferente... – almo Jan 10 '22 at 22:40
  • Well, this thread also points to changes, which have been made in Rails: https://github.com/rails/rails/pull/6040/commits/18aa1ae29c3459a6b2c10c7634770209a72c6cfe - now the `encode_big_decimal_as_string` option is available, which should resolve your issue. – Dmitry Kravtsov Jan 10 '22 at 23:10

0 Answers0