A simple problem, please help

I have a recipe and a category table, as per

recipe belongs to a category
a category has many recipes

in my controller, how do i get an instance variable that contains all
recipes that belong to the category ‘sweet’?

please please help
thanks

Hi –

On Fri, 17 Nov 2006, pingu wrote:

I have a recipe and a category table, as per
Radar – O’Reilly

recipe belongs to a category
a category has many recipes

in my controller, how do i get an instance variable that contains all
recipes that belong to the category ‘sweet’?

Probably something like this:

@recipes = Category.find_by_name(“sweet”).recipes

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Thanks very much David, but i get the followng error, any idea?

undefined method `find_by_name’ for Category:Class

Just figured it out, replace the ‘name’ part, with the field in the
database, thats great, thank you so much.

With

@recipes = Category.find_by_name(“sweet”).recipes

what would i do if i wanted to pass in “sweet” as a variable? can i use
:id with this?

pingu wrote:

With

@recipes = Category.find_by_name(“sweet”).recipes

what would i do if i wanted to pass in “sweet” as a variable? can i use
:id with this?

If you are using the normal ID field of a model then you on need

@recipes = Category.find(@params[:id]).recipes

.find works with the ID. The others basically extend the
.find_by_ pattern.

thankyou