Rails NoMethodError: undefined method new for BigDecimal:Class

John Cowan picture John Cowan · Feb 14, 2020 · Viewed 11k times · Source

My app is on:

ruby-2.6.4 Rails 4.2.8 activerecord-4.2.8

I'm upgrading a rails app to ruby-2.6.4 When running a rake task in development, for testing, I am getting errors for BigDecimal() and .../vendor/bundle/ruby/2.6.0/gems/ruby-oci8/...

ArgumentError: invalid value for BigDecimal(): "0.E+00"
/sha/git/sha_human_resources/shared/bundle/ruby/2.6.0/gems/ruby-oci8-2.1.8/lib/oci8/bindtype.rb:35:in `BigDecimal'
...
.../vendor/bundle/ruby/2.6.0/gems/ruby-oci8-2.1.8/lib/oci8/bindtype.rb:216: warning: constant ::Fixnum is deprecated
.../vendor/bundle/ruby/2.6.0/gems/ruby-oci8-2.1.8/lib/oci8/bindtype.rb:219: warning: constant ::Bignum is deprecated
.../vendor/bundle/ruby/2.6.0/gems/ruby-oci8-2.1.8/lib/oci8/compat.rb:73: warning: constant ::Fixnum is deprecated

I found a couple threads about updating the bigdecimal and oci8 gems, so tried a couple versions and found these helped: git diff Gemfile:

-gem 'ruby-oci8', '~> 2.1.5'
+gem 'ruby-oci8', '~> 2.2.8'

and added a bigdecimal gem:

+gem "bigdecimal", ">= 2.0.0"

Now when I run my rake task, I get this error:

rake aborted!
NoMethodError: undefined method `new' for BigDecimal:Class
.../vendor/bundle/ruby/2.6.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object/duplicable.rb:111:in `<class:BigDecimal>'
.../vendor/bundle/ruby/2.6.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object/duplicable.rb:106:in `<top (required)>'
.../vendor/bundle/ruby/2.6.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object.rb:3:in `require'
.../vendor/bundle/ruby/2.6.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object.rb:3:in `<top (required)>'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails/configuration.rb:2:in `require'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails/configuration.rb:2:in `<top (required)>'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails/railtie.rb:2:in `require'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails/railtie.rb:2:in `<top (required)>'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails/engine.rb:1:in `require'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails/engine.rb:1:in `<top (required)>'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails/application.rb:7:in `require'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails/application.rb:7:in `<top (required)>'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails.rb:11:in `require'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails.rb:11:in `<top (required)>'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails/all.rb:1:in `require'
.../vendor/bundle/ruby/2.6.0/gems/railties-4.2.8/lib/rails/all.rb:1:in `<top (required)>'
.../config/application.rb:3:in `require'
.../config/application.rb:3:in `<top (required)>'
.../Rakefile:6:in `require'
.../Rakefile:6:in `<top (required)>'
/usr/local/rvm/rubies/ruby-2.6.4/bin/bundle:30:in `block in <main>'
/usr/local/rvm/rubies/ruby-2.6.4/bin/bundle:22:in `<main>'
/usr/local/rvm/gems/ruby-2.6.4/bin/ruby_executable_hooks:24:in `eval'
/usr/local/rvm/gems/ruby-2.6.4/bin/ruby_executable_hooks:24:in `<main>'

Doing more Google surfing, I found there is not a lot of info on solving this. I did see a couple mentions of upgrading to the latest version of rails. Is this a must to solve this? Is there a monkeypatch or fix out there that can solve/fix/prevent this until we are ready to upgrade our rails version? I feel we will always be chasing the carrot-on-the-stick that is the latest version of rails.

Thanks for any advice.

Answer

adil hussain picture adil hussain · Mar 2, 2020

As per the documentation of BigDecimal, for all Ruby versions 2.6.x, you should be using BigDecimal with a specific version:

The differences among versions are given below:

version  characteristics                                            Supported ruby version range
2.0.0    You cannot use BigDecimal.new and do subclassing           2.4 ..
1.4.x    BigDecimal.new and subclassing always prints warning.      2.3 .. 2.6
1.3.5    You can use BigDecimal.new and subclassing without warning .. 2.5

You can select the version you want to use using gem method in Gemfile or scripts. For example, you want to stick bigdecimal version 1.3.5, it works file to put the following gem call in you Gemfile.

gem 'bigdecimal', '1.3.5'