A simple problem, please help

I have a recipe and a category table, as per
http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=5

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
http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=5

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] http://www.manning.com/black | [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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs