Search code examples
ruby-on-railsbigdecimal

BigDecimal attribute changes every page reloading


I was noticed that BigDecimal attribute in my db row changing every page reloading.

sum: #<BigDecimal:4add7d0,'0.9E0',9(36)>
ctrl+r
sum: #<BigDecimal:639f200,'0.9E0',9(36)>
ctrl+r
sum: #<BigDecimal:594ceb0,'0.9E0',9(36)>

Is it ok?


Solution

  • It looks to me like the Object ID is changing, but not the value; all of the results you list use the format #<BigDecimal:xxxxxxx,'0.9E0',9(36)>

    Each time you reload the page, Rails is creating a new BigDecimal object instance, but with the same data in it. Here's a similar example, using the irb console:

    irb(main):004:0> require 'bigdecimal'
    => true
    irb(main):005:0> BigDecimal(9)
    => #<BigDecimal:2aadb50,'0.9E1',9(36)>
    irb(main):006:0> BigDecimal(9)
    => #<BigDecimal:2764ab8,'0.9E1',9(36)>
    irb(main):007:0> BigDecimal(9)
    => #<BigDecimal:25c3638,'0.9E1',9(36)>
    irb(main):008:0>