计算不同货币的付款

时间:2013-08-23 15:00:05

标签: ruby-on-rails ruby

我的表项目有“USD”或“EUR”字符串 我在我的控制器变量中创建了

 def index
    ...
    @currencydebt = CurrencyRate.find( :all, 
     :conditions => ["created_at < ? and currency_id = ?", Date.today, 2], 
     :order => ["created_at DESC"], :limit => 1 )
    ...
    end

女巫找到最后一个货币USD

**在我看来**

<% for res in @items %>
     <% for loc in @currencydebt %>
       <%= number_with_precision(((loc.rate.round(2)  * res.payment)), 2) %>
     <% end %>
<% end %>

结果显示货币为USD的香港 但是,如果在物品中有字符串USD,那么我该怎么办呢?如果物品中有欧元,如果物品中有欧元,则会产生欧元吗?

我的ActiveRecord

create_table "items", :force => true do |t|
  t.string    "name"
  t.string    "currency"
  t.decimal    "payment"
  ...
end

create_table "currency_rates", :force => true do |t|
  t.integer "currency_id"
  t.decimal "rate"
  ...
end  

create_table "currencies", :force => true do |t|
  t.string "name"     #USD or EUR
  t.string "short_name"
end

1 个答案:

答案 0 :(得分:1)

首先,处理货币总是很棘手。有一些宝石可以缓解this之类的痛苦。

要解决您当前的问题,您必须先确定基础货币。因此,假设您希望将美元作为基础货币,则使用gem在您想要的任何货币之间切换。

相关问题