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:19:40
From:   jfister
Response to: "Showing recipes in a category" code

I have to agree 100%. It's easy with an application like Rails for beginning programmers to wreak havoc on a server by not being careful with their database queries. Unfortunately, this tutorial does just that in the "Showing recipes in a category" code.


The working 'list' method of the Recipe controller should look like this:


def list
@category = @params['category']
@recipes=Recipe.find_all("category_id="+@category)
end


As pointed out by petercooper, this allows the database server to weed out all records that do not match, rather than loading them all into memory so that the view can filter them.

Main Topics Oldest First

Showing messages 1 through 1 of 1.

  • Curt Hibbs photo "Showing recipes in a category" code
    2005-03-08 08:36:56  Curt Hibbs | O'Reilly AuthorO'Reilly Blogger [View]

    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.