money-rails,Money#==仅支持零数字

时间:2018-09-15 20:09:14

标签: ruby-on-rails ruby currency money-rails

我在Rails应用程序中使用money-rails gem。到目前为止,它一直运行良好,但是最近我开始出现以下错误:

Money#== supports only zero numerics

我不确定这是什么原因或如何解决。我最近运行过bundle update,所以我想某些内容已经更新。我的gemfile看起来像这样:

gem 'money-rails', '~>1'
gem 'eu_central_bank', "~>1.3.0"

我的实现如下:

# model
monetize :price_in_cents

# fetch / converting currencies
eu_bank = EuCentralBank.new
Money.default_bank = eu_bank
eu_bank.update_rates
converted_price = eu_bank.exchange_with(Money.new(price_to_convert * 100, from_currency), to_currency)

正如我之前提到的那样,这已经起作用了,所以我不确定是什么原因破坏了它。

有什么想法吗?

更新

为了测试,我尝试了以下操作。

money = Money.new(100, from_currency)

然后我得到了和以前一样的错误。但是,如果我尝试过:

money = Money.new(0, from_currency)

似乎可行。我觉得有点奇怪。

更新

这是我尝试保存记录时的回溯记录:

[“ / Users / [user] /。rvm / gems / ruby​​-2.5.1 / gems / money-6.11.3 / lib / money / money / arithmetic.rb:70:in =='", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activemodel-5.1.6/lib/active_model/validations/numericality.rb:22:in中! ='“,” / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activemodel-5.1.6 / lib / active_model / validations / numericality.rb:22:in validate_each'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activemodel-5.1.6/lib/active_model/validator.rb:150:in块在验证中”,“ / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activemodel-5.1.6 / lib / active_model / validator.rb:147:在each'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activemodel-5.1.6/lib/active_model/validator.rb:147:in中验证” “,” / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activesupport-5.1.6 / lib / active_support / callbacks.rb:413:在block in make_lambda'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:197:in块中(2个级别)在暂停中“”,“ / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activesupport-5.1.6 / lib / active_support / callbacks.rb:601:in block (2 levels) in default_terminator'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:600:in catch' “,“ / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activesupport-5.1.6 / lib / active_support / callbacks.rb:600:in block in default_terminator'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:198:in中的阻止暂停” ,“ / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activesupport-5.1.6 / lib / active_support / callbacks.rb:507:in block in invoke_before'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:507:in中的每个”,” /用户/ [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activesupport-5.1.6 / lib / active_support / callbacks.rb:507:在invoke_before'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:130:in run_callbacks'“,” / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activesupport-5.1.6 / lib / active_support / callbacks.rb:827:在_run_validate_callbacks'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activemodel-5.1.6/lib/active_model/validations.rb:405:in run_validations!'“,” /Users/[user]/.rvm/gems/ruby-2.5.1/gems/ activemodel-5.1.6 / lib / active_model / validations / callbacks.rb:114:在block in run_validations!'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:97:in run_callbacks'“,” /Users/[user]/.rvm/gems/ruby-2.5.1/gems/activesupport中-5.1.6 / lib / active_support / callbacks.rb:827:in _run_validation_callbacks'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activemodel-5.1.6/lib/active_model/validations/callbacks.rb:114:in run_validations!'“,” /Users/[user]/.rvm/gems/ruby-2.5.1/gems/activemodel-5.1 .6 / lib / active_model / validations.rb:335:in valid?'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activerecord-5.1.6/lib/active_record/validations.rb:65:in valid?'“,”,“ / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activerecord-5.1.6 /lib/active_record/validations.rb:82:在perform_validations'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activerecord-5.1.6/lib/active_record/validations.rb:50:in中保存!'“,” / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activerecord-5.1.6 / lib /active_record/attribute_methods/dirty.rb:43:在save!'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activerecord-5.1.6/lib/active_record/transactions.rb:313:in中保存!'“,” /Users/[user]/.rvm/gems/ruby-2.5.1/gems/activerecord-5.1.6 /lib/active_record/transactions.rb:384:in block in with_transaction_returning_status'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in交易中的块“”,“ / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activerecord-5.1.6 / lib / active_record / connection_adapters / abstract / transaction.rb:194:in block in within_new_transaction'", "/Users/[user]/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/monitor.rb:226:in mon_synchronize'“,” / Users / [user ] /。rvm / gems / ruby​​-2.5.1 / gems / activerecord-5.1.6 / lib / active_record / connection_adapters / abstract / transaction.rb:191:in within_new_transaction'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in transaction'“,” / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activerecord-5.1.6 / lib / active_record / transactions.rb:210:in transaction'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activerecord-5.1.6/lib/active_record/transactions.rb:381:in with_transaction_returning_status'“,” / Users / [用户] / .rvm / gems / ruby​​-2.5.1 / gems / activerecord-5.1.6 / lib / active_record / transactions.rb:313:在save!'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activerecord-5.1.6/lib/active_record/suppressor.rb:46:in中保存!'“”,“ / Users / [user] / Documents / Development / wondery / [appname] /lib/importers/bts_wholesaler_importer.rb:245:in block in set_supported_currencys_for_product_variant'", "/Users/[user]/Documents/Development/wondery/[appname]/lib/importers/bts_wholesaler_importer.rb:240:in each'“,” /用户/ [用户] / Documents / Development / wondery / [appname] / lib / importers / bts_wholesaler_importer.rb:240:in set_supported_currencys_for_product_variant'", "/Users/[user]/Documents/Development/wondery/[appname]/lib/importers/bts_wholesaler_importer.rb:224:in set_up_product_variant'“,” /Users/[user]/Documents/Development/wondery/[appname]/lib/importers/bts_wholesaler_importer.rb:208:in match_product_variant_to_product'", "/Users/[user]/Documents/Development/wondery/[appname]/lib/importers/bts_wholesaler_importer.rb:84:in在“ get_products_from_api_data”中阻止,“ / Users / [用户] / Documents / Development / wondery / [appname] /lib/importers/bts_wholesaler_importer.rb:75:在map'", "/Users/[user]/Documents/Development/wondery/[appname]/lib/importers/bts_wholesaler_importer.rb:75:in get_products_from_api_data”中,“ / Users / [用户] / Documents / Development / wondery / [appname] / lib / importers / bts _wholesaler_importer.rb:25:在import'", "(irb):2:in irb_binding'“,” / Users / [用户] /。rvm / rubies / ruby​​-2.5.1 / lib / ruby​​ / 2.5.0 / irb / workspace.rb:85中:在eval'", "/Users/[user]/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/irb/workspace.rb:85:in中评估“”,“ //用户/ [用户] /。rvm / rubies / ruby​​-2.5.1 / lib / ruby​​ / 2.5.0 / irb / context.rb:380:在{{1 }}在eval_input中阻止(2级)'“,” / Users / [用户] /。rvm / rubies / ruby​​-2.5.1 / lib / ruby​​ / 2.5.0 / irb.rb:623:in {{1} }在eval_input中阻止'“,” / Users / [用户] /。rvm / rubies / ruby​​-2.5.1 / lib / ruby​​ / 2.5.0 / irb / ruby​​-lex.rb:246:在evaluate'", "/Users/[user]/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/irb.rb:491:in中循环”“,” //用户/ [用户] /。rvm / ruby​​es / ruby​​-2.5.1 / lib / ruby​​ / 2.5.0 / irb / ruby​​-lex.rb:232:在signal_status'", "/Users/[user]/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/irb.rb:488:in中捕获'“ ,“ / Users / [用户] /。rvm / rubies / ruby​​-2.5.1 / lib / ruby​​ / 2.5.0 / irb / ruby​​-lex.rb:231:in block (2 levels) in each_top_level_statement'", "/Users/[user]/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/irb/ruby-lex.rb:232:in eval_input'”,“ /用户/ [用户] /。rvm / rubies / ruby​​-2.5.1 / lib / ruby​​ / 2.5.0 / irb.rb:428:在block in each_top_level_statement'", "/Users/[user]/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/irb/ruby-lex.rb:231:in中捕获'“,” /用户/ [用户] /。 rvm / rubies / ruby​​-2.5.1 / lib / ruby​​ / 2.5.0 / irb.rb:427:in each_top_level_statement'", "/Users/[user]/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/irb.rb:487:in start'“,” / Users / [用户] /。rvm / gems / ruby​​-2.5 .1 / gems / railties-5.1.6 / lib / rails / commands / console / console_command.rb:62:in block in run'", "/Users/[user]/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/irb.rb:427:in start'“,” /Users/[user]/.rvm/gems/ruby- 2.5.1 / gems / railties-5.1.6 / lib / rails / commands / console / console _command.rb:97:in run'", "/Users/[user]/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/irb.rb:383:in run'“,” / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / thor-0.20.0 / lib / thor / invocation.rb :126:in start'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/railties-5.1.6/lib/rails/commands/console/console_command.rb:17:in dispatch'“,” /Users/[user]/.rvm/gems/ruby-2.5.1/gems/railties-5.1.6/lib/rails/command/base.rb: 63:in perform'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/thor-0.20.0/lib/thor/command.rb:27:in调用'“,” / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / railties-5.1.6 / lib / rails / commands.rb:16:in invoke_command'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/thor-0.20.0/lib/thor.rb:387:in要求““,” //用户/ [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activesupport-5.1.6 / lib / active_support / dependencies.rb:292:in {{1 }} load_dependency'“,” / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activesupport-5.1.6 / lib / active_support / dependencies.rb:292:in perform'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/railties-5.1.6/lib/rails/command.rb:44:in' “,” / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activesupport-5.1.6 / lib / active_support / dependencies.rb:286:在<top (required)>'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in中阻止加载” ,“ / Users / [用户] /。rvm / gems / ruby​​-2.5.1 / gems / activesupport-5.1.6 / lib / active_support / dependencies.rb:258:in block in require'", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in load'”,“ /用户/ [用户] /。rvm / rubies / ruby​​-2.5.1 / lib / ruby​​ / site_ruby / 2.5.0 / ruby​​gems / core_ext / kernel_require.rb:59:in require'", "/Users/[user]/Documents/Development/wondery/[appname]/bin/rails:9:in require'“,” -e :1:in''“]

3 个答案:

答案 0 :(得分:3)

该错误来自模型的数字验证。

Money gem不允许将货币对象与数字进行比较,除非您的数字为零。否则,它希望您将“金钱与金钱”进行比较。

在irb中,您可以尝试:

```红宝石

2.5.1 :006 > Money.new(1000, "USD") != Money.new(1000, "USD")
 => false 

2.5.1 :007 > Money.new(1000, "USD") != 1000
Traceback (most recent call last):
        4: from /Users/andi/.rvm/rubies/ruby-2.5.1/bin/irb:11:in `<main>'
        3: from (irb):7
        2: from (irb):7:in `!='
        1: from /Users/andi/.rvm/gems/ruby-2.5.1/gems/money-6.12.0/lib/money/money/arithmetic.rb:70:in `=='
ArgumentError (Money#== supports only zero numerics)

2.5.1 :008 > Money.new(1000, "USD") != 0
 => true 

```

我相信这个错误是有道理的,因为您无法真正比​​较任意数字与给定货币下的金额。

Money-rails宝石再次带有其自己的验证器:

https://github.com/RubyMoney/money-rails#numericality-validation-options

您的回溯表明您正在使用rails数字验证器。

答案 1 :(得分:0)

编写规范时,您可能还会遇到此错误:

//works fine
...
canvas.width  = 640;
canvas.height = 100;
ctx.drawImage(img, 0, 0);
...

//don't  works
...
canvas.width  = 1024;
canvas.height = 768;
ctx.drawImage(img, 0, 0);
...

答案 2 :(得分:0)

Money Rails 会自动将“_cents”添加到您正在获利的属性中。然后它创建了一个 model.price 的实例,其中包括

<Money fractional:1500 currency:DKK>

因此,您不能执行 model.price + 10,因为它会导致您遇到的错误。 因此,您可能希望通过 price_to_convert 或您正在使用的任何其他值获利,以便将它们汇总等。

创建一个新的迁移文件,例如 rails g 迁移 AddMoneytizeToOrders

更改迁移文件,使其读取如下(将列/属性更改为您的值)

class AddMonetizeToOrder < ActiveRecord::Migration[6.0]
  def change
    add_monetize :orders, :price_to_convert, currency: { present: false }
  end
end

在你的模型中你必须包含

monetize :price_to_convert_cents

然后你可以使用例如(rails c) Order.last.price_to_convert + Model.last.price 等

请注意,您不能使用 self.[:price_to_convert],因为money rails 不会理解它,并且您会收到错误

money rails ActiveModel::MissingAttributeError (can't write unknown attribute...

因此改用 self.price_to_convert