[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Getting values from array of arrays

Allen Walker

9/11/2008 12:57:00 AM

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
--
Posted via http://www.ruby-....

5 Answers

David A. Black

9/11/2008 1:05:00 AM

0

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.r... for details and updates!

Heesob Park

9/11/2008 1:18:00 AM

0

Hi,

2008/9/11 Allen Walker <auswalk@gmail.com>:
> 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

Allen Walker

9/11/2008 1:53:00 AM

0

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?


David A. Black wrote:
> 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 :-)
>
>>
>> @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

--
Posted via http://www.ruby-....

David A. Black

9/11/2008 10:15:00 AM

0

Hi --

On Thu, 11 Sep 2008, Allen Walker wrote:

> 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.r... for details and updates!

Phlip

9/11/2008 11:36:00 AM

0

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