Getting values from array of arrays

A

Allen Walker

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
 
D

David A. Black

Hi --

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

Heesob Park

Hi,

2008/9/11 Allen Walker said:
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".
You can use assoc like this:

id = ARTICLE_CATEGORIES.assoc("Article").last

Regards,

Park Heesob
 
A

Allen Walker

Thanks. I was following along in Pragmatic - Agile Web Development 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?

Hi --

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
 
D

David A. Black

Hi --

Thanks. I was following along in Pragmatic - Agile Web Development 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?

Yes. I don't think the controller should know the details of the
ARTICLE_CATEGORIES data structure.


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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top