From: "David A. Black" Date: 2008-09-11T10:04:39+09:00 Subject: Re: Getting values from array of arrays Hi -- On Thu, 11 Sep 2008, Allen Walker wrote: > I have a drop down list in my view. In my model I have the following: I'm guessing this is a Rails application :-) > ARTICLE_CATEGORIES = [ > ["News", 1], ["Article", 2], ["Review", 3] > ] > > validates_inclusion_of :category, :in => ARTICLE_CATEGORIES.map { |disp, > value| value } > > I want to extract out all "Article" types in my controller. > > So in my controller: (I have a 'category' field in my 'articles' table) > > @news = Article.find_by_category(ARTICLE::ARTICLE_CATEGORIES.find...??) > > Not sure how to do this. I want to get the ID (in this case 2) from the > ARTICLES_CATEGORIES array but I want to extract it out of this array > using the key "Article". You should use a hash, rather than an array. ARTICLE_CATEGORIES = { "News" => 1, "Article" => 2, "Review" => 3 } @news = Article.find_by_category(ARTICLE_CATEGORIES["News"]) and probably push some of this down into the model to unclutter your controller. You could also line them up in an array and use #index, but the hash is more transparent and maintainable. And I imagine there's a plugin somewhere that does all of this... but the main lesson here is the hash. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL Advancing with Rails January 19-22 Fort Lauderdale, FL * * Co-taught with Patrick Ewing! See http://www.rubypal.com for details and updates!