Women in Technology

Hear us Roar



Article:
  Rolling with Ruby on Rails, Part 2
Subject:   "Showing recipes in a category" code
Date:   2005-03-08 08:36:56
From:   curth
Response to: "Showing recipes in a category" code

Even better, I could have used:



def list
@category = @params['category']
@recipes=Recipe.find_by_category(@category)
end

This is an example of dynamic finders, where "find_by_" can be appended with a column name.


Main Topics Oldest First

Showing messages 1 through 1 of 1.

  • "Showing recipes in a category" code
    2005-03-10 02:21:34  aeden [View]

    Unfortunately the dynamic find method didn't work on my system. However this does:

    @recipes = Recipe.find_all ["category_id = ?", @category]

    I did have to make some changes to the HTML. First of all the category passed as the argument from the list page should be the category ID and not the name. Second of all you will need to remove the if statement in list.rhtml. I spent 10 minutes trying to figure out why it wasn't showing the filtered recipe list before I realized that this was the problem.