Search code examples
ruby-on-railspostgresqlruby-on-rails-4money-rails

Entering in large dollar values into rails money object


I have built an application running rails 4.1 and ruby 1.9.3 that uses the money-rails gem. I'm encountering an issue when I input large dollar values into form fields and save them to my PG database. The error is the following:

PG::NumericValueOutOfRange: ERROR: value "9900000000" is out of range for type integer

The PG docs show the max value of an integer being +2147483647. I would like to be able to use the money-rails gem but be able to enter larger numbers.

As far as a solution goes, I understand that the column type in PG should be a bigint, however I'm not how to enable money-rails to support storing numbers as bigint instead of integers.


Solution

  • Here in the money-rails README it shows that you can configure it to use other data types:

    # Default ActiveRecord migration configuration values for columns:
    #
    # config.amount_column = { prefix: '',           # column name prefix
    #                          postfix: '_cents',    # column name  postfix
    #                          column_name: nil,     # full column name (overrides prefix, postfix and accessor name)
    #                          type: :integer,       # column type
    #                          present: true,        # column will be created
    #                          null: false,          # other options will be treated as column options
    #                          default: 0
    #                        }
    

    Notice there's a type: :integer option. Try changing that to :bigint.