# A simple problem, please help

**URL:** <https://www.ruby-forum.com/t/a-simple-problem-please-help/76302>\
**Category:** Rails\
**Created:** [November 17, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/a-simple-problem-please-help/76302 "2006-11-17T13:19:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9a28c/32.png) [@pingu](https://www.ruby-forum.com/u/pingu)\
**Post date:** [November 17, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/a-simple-problem-please-help/76302/1 "2006-11-17T13:19:38Z")

</div>

I have a recipe and a category table, as per

> **[Radar - O’Reilly](https://www.oreilly.com/radar/?page=5)**
>
> Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

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

---

<div class="post-metadata">

**Author:** ![pingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9a28c/32.png) [@pingu](https://www.ruby-forum.com/u/pingu)\
**Post date:** [November 17, 2006, 1:23pm UTC](https://www.ruby-forum.com/t/a-simple-problem-please-help/76302/2 "2006-11-17T13:23:57Z")

</div>

Hi –

On Fri, 17 Nov 2006, pingu wrote:

> I have a recipe and a category table, as per  
> [Radar – O’Reilly](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 | removed\_email\_address@domain.invalid  
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](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![pingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9a28c/32.png) [@pingu](https://www.ruby-forum.com/u/pingu)\
**Post date:** [November 17, 2006, 1:29pm UTC](https://www.ruby-forum.com/t/a-simple-problem-please-help/76302/3 "2006-11-17T13:29:38Z")

</div>

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

undefined method `find\_by\_name’ for Category:Class

---

<div class="post-metadata">

**Author:** ![pingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9a28c/32.png) [@pingu](https://www.ruby-forum.com/u/pingu)\
**Post date:** [November 17, 2006, 1:34pm UTC](https://www.ruby-forum.com/t/a-simple-problem-please-help/76302/4 "2006-11-17T13:34:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9a28c/32.png) [@pingu](https://www.ruby-forum.com/u/pingu)\
**Post date:** [November 17, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/a-simple-problem-please-help/76302/5 "2006-11-17T18:56:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9a28c/32.png) [@pingu](https://www.ruby-forum.com/u/pingu)\
**Post date:** [November 18, 2006, 5:25am UTC](https://www.ruby-forum.com/t/a-simple-problem-please-help/76302/6 "2006-11-18T05:25:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9a28c/32.png) [@pingu](https://www.ruby-forum.com/u/pingu)\
**Post date:** [November 18, 2006, 11:17am UTC](https://www.ruby-forum.com/t/a-simple-problem-please-help/76302/7 "2006-11-18T11:17:22Z")

</div>

thankyou
