Topic: Simpe Problem get a value from a database.


I am doing an addition to a tutorial I did. The tutorial is a cook book.

I have in my MySQL database a table called categories with 3 categories in.

id & cat are my fields.

I also have a table called recipes which has cat_id as one of the field names.

How do I make a page which shows all the recipes from a certain category?


Re: Simpe Problem get a value from a database.

There's many ways,
  first way:

@recipes = Recipe.find_all_by_cat_id(params[:cat_id]) # assuming you're passing the selected category by id, etc

Do your Recipe and Category classes have belongs_to and has_many declarations, respectively?  Then it might be easier to do
@recipes = Category.find(params[:cat_id]).recipes

In both cases this would be added to a controller (probably the Recipe controller) and you could probably use a
  render :template=>'list'

call to re-use your existing page for display of the filtered recipes.

Last edited by NielsenE (2006-07-08 16:44:55)

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Simpe Problem get a value from a database.


The first one works perfectly!