在 rails 货币对象中输入大的美元值
Entering in large dollar values into rails money object
我构建了一个使用 money-rails gem 的应用程序 运行 rails 4.1 和 ruby 1.9.3。当我在表单字段中输入大量美元值并将它们保存到我的 PG 数据库时,我遇到了一个问题。错误如下:
PG::NumericValueOutOfRange: ERROR: value "9900000000" is out of range for type integer
PG docs显示整数的最大值为+2147483647。我希望能够使用 money-rails gem 但能够输入更大的数字。
就解决方案而言,我知道 PG 中的列类型应该是 bigint,但是我不知道如何启用 money-rails 来支持将数字存储为 bigint 而不是整数。
在 money-rails README 中显示您可以将其配置为使用其他数据类型:
# 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
# }
注意有一个 type: :integer
选项。尝试将其更改为 :bigint
.
我最近有类似的问题(ruby 2.0 和 rails 4.1)。唯一需要的是创建迁移以支持 8 字节整数,即 bigint:
change_column :order_items, :unit_price_cents, :integer, :limit => 8
而且效果很好。我的情况是我还需要支持 4 位小数,所以我必须按如下方式重新创建美元货币:
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
我构建了一个使用 money-rails gem 的应用程序 运行 rails 4.1 和 ruby 1.9.3。当我在表单字段中输入大量美元值并将它们保存到我的 PG 数据库时,我遇到了一个问题。错误如下:
PG::NumericValueOutOfRange: ERROR: value "9900000000" is out of range for type integer
PG docs显示整数的最大值为+2147483647。我希望能够使用 money-rails gem 但能够输入更大的数字。
就解决方案而言,我知道 PG 中的列类型应该是 bigint,但是我不知道如何启用 money-rails 来支持将数字存储为 bigint 而不是整数。
在 money-rails README 中显示您可以将其配置为使用其他数据类型:
# 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
# }
注意有一个 type: :integer
选项。尝试将其更改为 :bigint
.
我最近有类似的问题(ruby 2.0 和 rails 4.1)。唯一需要的是创建迁移以支持 8 字节整数,即 bigint:
change_column :order_items, :unit_price_cents, :integer, :limit => 8
而且效果很好。我的情况是我还需要支持 4 位小数,所以我必须按如下方式重新创建美元货币:
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