Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-797

Conversion to BigDecimal fails (introduced by commit 5286a42ae841fdb91d61/First refactoring of Model#read_attribute)

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      The problem is that BigDecimal gets stored to database as a string, but when reading it does not get converted back to BigDecimal. This bug was introduced by commit 5286a42ae841fdb91d61. The following simple example fails:

      class Product
      include Mongoid::Document
      field :price, :type => BigDecimal
      field :vat_rate, :type => BigDecimal
      def vat_rate_fraction
      (self.vat_rate / 100.0) + 1.0
      end
      end

      spec example:

      describe Product do
      it "calculates VAT inclusive price" do
      product = Product.create(:price => 1999, :vat_rate => 20)
      product.reload.vat_rate_fraction.should == BigDecimal.new('1.2')
      end
      end

      fail:

      Failures:

      1) Product calculates VAT inclusive price
      Failure/Error: product.reload.vat_rate_fraction.should == BigDecimal.new('1.2')
      NoMethodError:
      undefined method `/' for "20":String

      1. ./app/models/product.rb:7:in `vat_rate_fraction'
      2. ./spec/models/product_spec.rb:6:in `block (2 levels) in <top (required)>'

      Finished in 0.09278 seconds
      1 example, 1 failure

            Assignee:
            Unassigned Unassigned
            Reporter:
            jzajpt Ji?í Zajpt
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              None
              None
              None
              None