2

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.

Questifer
  • 1,113
  • 3
  • 18
  • 48
  • I always thought `money-rails` was for dealing with intricacies like cents. If you have huge numbers, maybe you could skip using the gem and just manipulate in whole dollars stored as `bigint`. – steve klein May 04 '15 at 21:29

2 Answers2

3

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.

DiegoSalazar
  • 13,361
  • 2
  • 38
  • 55
  • Thank you for pointing that out. Will I need to remove and re-add all my migrations for these changes to take effect? – Questifer May 04 '15 at 21:42
  • I think so. First do `rake db:rollback` and run the money-rails gem's rake task to install it again. – DiegoSalazar May 04 '15 at 22:05
  • After trying that, I'm receiving an error `add_column("amount_requested", :bigint, {}) StandardError: An error has occurred, this and all later migrations canceled: undefined method to_sym` amount_requested is the first column that I'm using the add_money helper on. – Questifer May 05 '15 at 01:11
  • That `add_column` call is missing the table name as the first argument. It should be: `add_column :name_of_your_table, :name_of_column, :bigint` – DiegoSalazar May 05 '15 at 17:47
1

I had similar question recently (with ruby 2.0 & rails 4.1). The only thing that was required is creating migration to support 8-byte integers, i.e. bigint:

 change_column :order_items, :unit_price_cents, :integer, :limit => 8 

And it just worked. I my case I needed to support also 4 decimals, so I had to recreate USD currency as follows:

MoneyRails.configure do |config|
  config.register_currency = {
     :priority            => 1,
     :iso_code            => "USD",
     :iso_numeric         => "840",
     :name                => "United States Dollar with subunit of 4 digits",
     :symbol              => "$",
     :symbol_first        => true,
     :subunit             => "Subcent",
     :subunit_to_unit     => 10000,
     :separator           => ".",
     :delimiter           => ","
  }
end
Alex P
  • 1,721
  • 1
  • 17
  • 18