2fas2c
September 19, 2006, 5:45am
1
I am pretty new to Ruby on Rails. Pardon me if it is a pretty basic
question.
I have a combo box built based on a Constant Array
PAYMENT_TYPES [
[“MasterCard”, “MC”],
[“Visa”, “VISA”],
[“American Express”, “AMEX”]
].freeze
I store the values (i.e. MC, VISA, AMEX) in my database…
When I display the information, I would like to display the keys (i.e.
MasterCard, Visa, American Express) to the user instead of MC, VISA,
AMEX.
How can I search PAYMENT_TYPES to get the keys given a value.
Any help is greatly appreciated.
Thanks
2fas2c
September 19, 2006, 6:11am
2
2fas2c wrote:
I am pretty new to Ruby on Rails. Pardon me if it is a pretty basic
question.
I have a combo box built based on a Constant Array
PAYMENT_TYPES [
[“MasterCard”, “MC”],
[“Visa”, “VISA”],
[“American Express”, “AMEX”]
].freeze
I store the values (i.e. MC, VISA, AMEX) in my database…
When I display the information, I would like to display the keys (i.e.
MasterCard, Visa, American Express) to the user instead of MC, VISA,
AMEX.
How can I search PAYMENT_TYPES to get the keys given a value.
Any help is greatly appreciated.
Thanks
Never mind. Figured it out…
PAYMENT_TYPES.rassoc(“MC”)[0] would give you “MasterCard”
Thanks
2fas2c
September 19, 2006, 6:58am
3
Never mind. Figured it out…
PAYMENT_TYPES.rassoc(“MC”)[0] would give you “MasterCard”
Thanks
That’s pretty inefficient. You could use a hash:
PAYMENT_TYPES = { ‘MC’=>‘MasterCard’, ‘VISA’=>‘Visa’,
‘AMEX’=>‘American Express’]
and then:
PAYMENT_TYPES[‘MC’]
You should strongly consider storing the display names in the database
as well. Your current setup distributes the business data across two
different layers (database and domain model), which is generally not a
good idea.
Max