Forum: Ruby on Rails question about options_for_select array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Szymon N. (Guest)
on 2006-03-08 18:13
Hi!

If i have something like this used to create select tag:

options = [["Dollar", "$"], ["Kroner", "DKK"]]

and later read value from the database and get "DKK", what is the
easiest way to get "Kroner" value?
Mark Reginald J. (Guest)
on 2006-03-09 01:54
(Received via mailing list)
szymek wrote:

> If i have something like this used to create select tag:
>
> options = [["Dollar", "$"], ["Kroner", "DKK"]]
>
> and later read value from the database and get "DKK", what is the
> easiest way to get "Kroner" value?

If you don't want to go all the way and create a currencies
table in the database, add a currency.rb file in the models
directory, containing:

class Currency
   OPTIONS = [["Dollar", "$"], ["Kroner", "DKK"]]

   def self.symbol_to_name( symbol )
      OPTIONS.rassoc(symbol).first
   end
end

Use like:

options_for_select( Currency::OPTIONS )
and
Currency.symbol_to_name( @amount.currency_symbol )




--
We develop, watch us RoR, in numbers too big to ignore.
This topic is locked and can not be replied to.