# Getting values from array of arrays

**URL:** <https://www.ruby-forum.com/t/getting-values-from-array-of-arrays/148268>\
**Category:** Ruby\
**Created:** [September 11, 2008, 3:03am UTC](https://www.ruby-forum.com/t/getting-values-from-array-of-arrays/148268 "2008-09-11T03:03:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [September 11, 2008, 3:03am UTC](https://www.ruby-forum.com/t/getting-values-from-array-of-arrays/148268/1 "2008-09-11T03:03:52Z")

</div>

I have a drop down list in my view. In my model I have the following:

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

Thanks

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [September 11, 2008, 3:11am UTC](https://www.ruby-forum.com/t/getting-values-from-array-of-arrays/148268/2 "2008-09-11T03:11:43Z")

</div>

Hi –

On Thu, 11 Sep 2008, Allen W. 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 🙂

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

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [September 11, 2008, 3:25am UTC](https://www.ruby-forum.com/t/getting-values-from-array-of-arrays/148268/3 "2008-09-11T03:25:11Z")

</div>

Hi,

2008/9/11 Allen W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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 can use assoc like this:

id = ARTICLE\_CATEGORIES.assoc(“Article”).last

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [September 11, 2008, 3:59am UTC](https://www.ruby-forum.com/t/getting-values-from-array-of-arrays/148268/4 "2008-09-11T03:59:26Z")

</div>

Thanks. I was following along in Pragmatic - Agile Web D. with  
Rails and they were using an Array of Arrays. But I changed it to a hash  
which makes more sense and it works.

I know this isn’t a rails forum but I am just commenting on your  
suggest.

So you don’t think:  
@news = Article.find( :all,  
:conditions =\> [“category = ?”,  
Article::ARTICLE\_CATEGORIES[“News”]])

should be in the controller but instead I should wrap that into a method  
in the model?

David A. Black wrote:

> Hi –
> 
> On Thu, 11 Sep 2008, Allen W. 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 🙂
> 
> > @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

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [September 11, 2008, 12:22pm UTC](https://www.ruby-forum.com/t/getting-values-from-array-of-arrays/148268/5 "2008-09-11T12:22:35Z")

</div>

Hi –

On Thu, 11 Sep 2008, Allen W. wrote:

> Article::ARTICLE\_CATEGORIES[“News”]])
> 
> should be in the controller but instead I should wrap that into a method  
> in the model?

Yes. I don’t think the controller should know the details of the  
ARTICLE\_CATEGORIES data structure.

David

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [September 11, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/getting-values-from-array-of-arrays/148268/6 "2008-09-11T13:40:41Z")

</div>

> > So you don’t think:  
> > @news = Article.find( :all,  
> > :conditions =\> [“category = ?”,  
> > Article::ARTICLE\_CATEGORIES[“News”]])
> > 
> > should be in the controller but instead I should wrap that into a method  
> > in the model?
> 
> Yes. I don’t think the controller should know the details of the  
> ARTICLE\_CATEGORIES data structure.

And it should use .find\_all\_by\_category …
