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
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
Hi,
2008/9/11 Allen W. [email protected]:
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.
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
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
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 …