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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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. > > ThanksNever mind. Figured it out.. PAYMENT_TYPES.rassoc("MC")[0] would give you "MasterCard" Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
> > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---