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

Model.inc won't take BigDecimal

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.2
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      I'm incrementing a field I've defined as BigDecimal:

      field :balance, type: BigDecimal, default: 0

      So I figured the most appropriate thing to do is convert the value to BigDecimal before passing it to #inc:
      def increment(amount)
      self.inc(:balance, amount.to_d * 100)
      end

      But I get the following error:

      NoMethodError:
      undefined method `bson_dump' for #<BigDecimal:29563f0,'0.1E6',9(36)>

      1. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/bson/extensions/hash.rb:36:in `block in bson_dump'
      2. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/bson/extensions/hash.rb:35:in `each'
      3. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/bson/extensions/hash.rb:35:in `bson_dump'
      4. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/bson/extensions/hash.rb:36:in `block in bson_dump'
      5. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/bson/extensions/hash.rb:35:in `each'
      6. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/bson/extensions/hash.rb:35:in `bson_dump'
      7. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/bson/document.rb:11:in `serialize'
      8. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/protocol/message.rb:136:in `serialize_update'
      9. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/protocol/message.rb:289:in `serialize'
      10. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/connection.rb:137:in `block in write'
      11. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/connection.rb:135:in `each'
      12. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/connection.rb:135:in `write'
      13. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/node.rb:518:in `block (2 levels) in flush'
      14. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/node.rb:113:in `ensure_connected'
      15. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/node.rb:517:in `block in flush'
      16. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/node.rb:532:in `logging'
      17. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/node.rb:516:in `flush'
      18. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/node.rb:505:in `process'
      19. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/node.rb:422:in `update'
      20. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/session/context.rb:76:in `block in update'
      21. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/session/context.rb:109:in `block in with_node'
      22. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/cluster.rb:139:in `block in with_primary'
      23. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/node.rb:158:in `ensure_primary'
      24. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/cluster.rb:138:in `with_primary'
      25. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/session/context.rb:108:in `with_node'
      26. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/session/context.rb:69:in `update'
      27. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/query.rb:305:in `block in update'
      28. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/session.rb:264:in `with'
      29. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/moped-1.1.3/lib/moped/query.rb:304:in `update'
      30. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.1/lib/mongoid/persistence/atomic/operation.rb:97:in `execute'
      31. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.1/lib/mongoid/persistence/atomic/inc.rb:22:in `block in persist'
      32. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.1/lib/mongoid/persistence/atomic/operation.rb:78:in `prepare'
      33. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.1/lib/mongoid/persistence/atomic/inc.rb:19:in `persist'
      34. /home/ofer/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-3.0.1/lib/mongoid/persistence/atomic.rb:82:in `inc'
      35. ./lib/mcbooks/ledger/base.rb:64:in `increment'

            Assignee:
            Unassigned Unassigned
            Reporter:
            odigity Ofer Nave
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: