货币不能强制进入Float

时间:2013-11-18 05:54:28

标签: ruby-on-rails ruby

我有一行代码导致头疼;

@amount = Currency.convert(current_user.currency, "USD", transaction.amount)

它会带来像这样的错误

Currency can't be coerced into Float

应用程序跟踪看起来像这样;

app/models/currency.rb:8:in `*'
app/models/currency.rb:8:in `convert'
app/controllers/paypal_controller.rb:35:in `create'

其中Currency.convert模型方法如下所示

  def self.convert(from,to,amount)
    from_rate = Currency.find_by_currency(from)
    to_rate = Currency.find_by_currency(to)
    usd = (amount/(from_rate.value.to_f))
    result = (usd*(to_rate.value.to_f))     
    return result
  end

错误是什么?

在控制台中运行

def convert(from,to,amount)
    from_rate = Currency.find_by_currency(from)
    to_rate = Currency.find_by_currency(to)
    usd = (amount/(from_rate.value.to_f))
    result = (usd*(to_rate.value.to_f))     
    return result
  end

然后;

convert("UGX","KES", 2000).to_f.round(0

作品。

货币模型看起来像这样;

class Currency < ActiveRecord::Base
  attr_accessible :currency, :value

      def self.convert(from,to,amount)
        from_rate = Currency.find_by_currency(from)
        to_rate = Currency.find_by_currency(to)
        usd_value  = (amount/(from_rate.value.to_f))
        result = (usd_value.to_f * (to_rate.value.to_f))        
        return result
      end
end

0 个答案:

没有答案