Search code examples
ruby-on-railsrubyruby-on-rails-4ruby-on-rails-4.2currency

Money Gem - undefined method `each' for nil:NilClass


I'm trying to use money gem in my machine but I'm getting following error.

I'm referring this link - https://github.com/RubyMoney/money/blob/master/README.md

Money.new(1000, "USD").exchange_to("EUR")
NoMethodError: undefined method `each' for nil:NilClass
    from /home/dev/new_hotspotting-backend/shared/bundle/ruby/2.3.0/gems/money-currencylayer-bank-0.5.3/lib/money/bank/currencylayer_bank.rb:93:in `update_rates'
    from /home/dev/new_hotspotting-backend/shared/bundle/ruby/2.3.0/gems/money-currencylayer-bank-0.5.3/lib/money/bank/currencylayer_bank.rb:142:in `expire_rates!'
    from /home/dev/new_hotspotting-backend/shared/bundle/ruby/2.3.0/gems/money-currencylayer-bank-0.5.3/lib/money/bank/currencylayer_bank.rb:108:in `get_rate'
    from /home/dev/new_hotspotting-backend/shared/bundle/ruby/2.3.0/gems/money-6.7.0/lib/money/bank/variable_exchange.rb:109:in `exchange_with'
    from /home/dev/new_hotspotting-backend/shared/bundle/ruby/2.3.0/gems/money-6.7.0/lib/money/money.rb:434:in `exchange_to'
    from (irb):17

I'm not understand why I'm getting this error. I'm using following version for money gems

money (6.9.0, 6.7.0)
money-currencylayer-bank (0.5.4)

enter image description here


Solution

  • try :

    object requires one to manually specify the exchange rate

    Money.add_rate("USD", "EUR", 0.5)
    Money.us_dollar(100).exchange_to("EUR")
    

    for more official GEM rapo

    output :

    2.3.0 :009 >   
    2.3.0 :010 >   require 'money'
     => true 
    2.3.0 :011 > Money.add_rate("USD", "EUR", 0.5)
     => 0.5 
    2.3.0 :012 > Money.us_dollar(100).exchange_to("EUR").currency
     => #<Money::Currency id: eur, priority: 2, symbol_first: true, thousands_separator: ., html_entity: &#x20AC;, decimal_mark: ,, name: Euro, symbol: €, subunit_to_unit: 100, exponent: 2, iso_code: EUR, iso_numeric: 978, subunit: Cent, smallest_denomination: 1> 
    2.3.0 :013 > 
    2.3.0 :014 >