0

I have table items wich have string "USD" or "EUR".
I create in my controller variable

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

witch find last currency USD

** in my view**

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

Аnd as a result showing paymant in currency USD.
But how can I do that would show up as a result if have string USD in items Payment result in USD, if have EUR in items Payment result in EUR?

my 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
Andrew
  • 91
  • 2
  • 9
  • Take a look at this Money gem and google currencies: http://stackoverflow.com/questions/10199301/rails-3-1-multiple-currencies or http://stackoverflow.com/questions/1019939/ruby-on-rails-best-method-of-handling-currency-money – rmagnum2002 Aug 23 '13 at 15:25
  • unfortunately i'm use rails 2.0 and don't use gems – Andrew Aug 26 '13 at 06:13

1 Answers1

1

Firstly, handling currency is always tricky. There are gems to ease the pain like this.

To solve your immediate problem though, you must decide first on a base currency. So assuming you want to keep USD as the base currency, then use the gem to switch between whatever currency you want.

Benjamin Tan Wei Hao
  • 9,621
  • 3
  • 30
  • 56