Searching an array of arrays

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 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs