| Article: |
Rolling with Ruby on Rails, Part 2 | |
| Subject: | Better way of filtering Recipes by Category | |
| Date: | 2005-04-07 20:18:11 | |
| From: | curth | |
|
Response to: Better way of filtering Recipes by Category
|
||
|
I completely agree that the best way to do this through the proper database queries within a method in the Recipe model.
|
||
Showing messages 1 through 2 of 2.
-
Better way of filtering Recipes by Category
2006-08-08 11:04:07 Steogede [View]
-
Better way of filtering Recipes by Category
2005-05-22 12:34:49 lewinke [View]
I'm not sure that this is any better but here is what I ended up with:
Adding this method to RecipeController
def list_category
@recipes = Recipe.find_all_by_category_id(@params['id'])
render_action "list"
end
And changing list.rhtml to make the category a link
<td><%= link_to recipe.category.name, :action => "list_category", :id => recipe.category.id %></td>
This could be HUGELY wrong since I've only spent about 45 minutes with rails and no time with ruby before that.



However, like you said the article was not about best practices. It gave me the chance to think on my own and see if I had learn't enough to write some of my own code. To my complete (albeit undeserved) surprise, I had picked up enough from your two tutorials (and a quick glance at the API docs) to make the changes I felt necessary.