I have created a STATES constant in my application-an array of hashes: STATES = [ {''code'' => ''AL'', ''name'' => ''Alabama''}, {''code'' => ''AK'', ''name'' => ''Alaska''}, {''code'' => ''AZ'', ''name'' => ''Arizona''}, {''code'' => ''AR'', ''name'' => ''Arkansas''}, {''code'' => ''CA'', ''name'' => ''California''}, etc. ] I''m storing the state code in the database with an object (i.e., event, address, etc.) When I retrieve, say, an Event from the database, I want to use the code ''AZ'' to look up the name ''Arizona'' and display it. I don''t seem to be able to get my head around how to accomplish this. Maybe it''s because it''s late and I''m really tired... --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
>> STATES.find{ |state| state["code"] == "AL" }["name"]=> "Alabama" On Jun 17, 3:51 am, partydrone <partydr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have created a STATES constant in my application-an array of hashes: > > STATES = [ > {''code'' => ''AL'', ''name'' => ''Alabama''}, > {''code'' => ''AK'', ''name'' => ''Alaska''}, > {''code'' => ''AZ'', ''name'' => ''Arizona''}, > {''code'' => ''AR'', ''name'' => ''Arkansas''}, > {''code'' => ''CA'', ''name'' => ''California''}, > etc. > ] > > I''m storing the state code in the database with an object (i.e., > event, address, etc.) When I retrieve, say, an Event from the > database, I want to use the code ''AZ'' to look up the name ''Arizona'' > and display it. > > I don''t seem to be able to get my head around how to accomplish this. > Maybe it''s because it''s late and I''m really tired...--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
The way you currently have your codes setup you can not locate anything by state code. Just use a hash as follows: states = {''AL'' => ''Alabama'', ''AK'' => ''Alaska'', ... then you can locate the name by states[code]. Michael On Jun 17, 1:51 am, partydrone <partydr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have created a STATES constant in my application-an array of hashes: > > STATES = [ > {''code'' => ''AL'', ''name'' => ''Alabama''}, > {''code'' => ''AK'', ''name'' => ''Alaska''}, > {''code'' => ''AZ'', ''name'' => ''Arizona''}, > {''code'' => ''AR'', ''name'' => ''Arkansas''}, > {''code'' => ''CA'', ''name'' => ''California''}, > etc. > ] > > I''m storing the state code in the database with an object (i.e., > event, address, etc.) When I retrieve, say, an Event from the > database, I want to use the code ''AZ'' to look up the name ''Arizona'' > and display it. > > I don''t seem to be able to get my head around how to accomplish this. > Maybe it''s because it''s late and I''m really tired...--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---