Rolling with Ruby on Rails, Part 2
Subject:   "Showing recipes in a category" code
Date:   2005-04-22 03:38:42
From:   xeal
Response to: "Showing recipes in a category" code

I also think that using the field is better - after all the might not be unique.

And the code simplifies, so you can write the whole RecipeController.list method as:

def list
@recipes = Recipe.find_all(@params["category_id"] ? ("category_id = " + @params["category_id"]) : nil)

The code above passes to Recipe.find_all a category_id, if @params["category_id"] is not empty or nil otherwise.

Of course, the statement from list.rhtml that renders the category needs to be changed as:

<td><%= link_to, :action => "list", :category_id => %></td>