# Finding a specific category

**URL:** <https://www.ruby-forum.com/t/finding-a-specific-category/128877>\
**Category:** Rails\
**Created:** [February 12, 2008, 11:13am UTC](https://www.ruby-forum.com/t/finding-a-specific-category/128877 "2008-02-12T11:13:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 12, 2008, 11:13am UTC](https://www.ruby-forum.com/t/finding-a-specific-category/128877/1 "2008-02-12T11:13:50Z")

</div>

For the following controller:

thufir@arrakis ~/oreilly-recipes-categories $ cat app/controllers/  
recipes\_controller.rb -n | head -n 18 | tail -n 3  
16 @recipe = Recipe.find(params[:id])  
17 @categories= Category.find\_all  
18 end  
thufir@arrakis ~/oreilly-recipes-categories $

[http://oreilly-recipes-categories.googlecode.com/svn/trunk/app/](http://oreilly-recipes-categories.googlecode.com/svn/trunk/app/)  
controllers/recipes\_controller.rb

I just want to pull up the Category instance which belongs to this  
Recipe  
instance. What should be on line 17 for that, please?

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 12, 2008, 11:48am UTC](https://www.ruby-forum.com/t/finding-a-specific-category/128877/2 "2008-02-12T11:48:47Z")

</div>

On 12 Feb 2008, at 10:13, Thufir wrote:

> [http://oreilly-recipes-categories.googlecode.com/svn/trunk/app/](http://oreilly-recipes-categories.googlecode.com/svn/trunk/app/)  
> controllers/recipes\_controller.rb
> 
> I just want to pull up the Category instance which belongs to this  
> Recipe  
> instance. What should be on line 17 for that, please?

@recipe.categories or @recipe.category depending on whether it’s a  
belongs\_to or a has\_many.

Fred

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 12, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/finding-a-specific-category/128877/3 "2008-02-12T21:10:39Z")

</div>

On Tue, 12 Feb 2008 10:48:11 +0000, Frederick C. wrote:

> @recipe.categories or @recipe.category depending on whether it’s a  
> belongs\_to or a has\_many.

And then in the view (show), basically, the @category instance can be  
used just as the @recipe instance is used?

thufir@arrakis ~/oreilly-recipes-categories $ cat app/controllers/  
recipes\_controller.rb -n | head -n 18 | tail -n 4  
15 def show  
16 @recipe = Recipe.find(params[:id])  
17 @category = @recipe.category  
18 end  
thufir@arrakis ~/oreilly-recipes-categories $ cat app/models/recipe.rb  
class Recipe \< ActiveRecord::Base  
belongs\_to :category  
end  
thufir@arrakis ~/oreilly-recipes-categories $

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 12, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/finding-a-specific-category/128877/4 "2008-02-12T21:20:42Z")

</div>

On 12 Feb 2008, at 20:09, Thufir wrote:

> On Tue, 12 Feb 2008 10:48:11 +0000, Frederick C. wrote:
> 
> > @recipe.categories or @recipe.category depending on whether it’s a  
> > belongs\_to or a has\_many.
> 
> And then in the view (show), basically, the @category instance can be  
> used just as the @recipe instance is used?

of course.

Fred
