# Problem accessing item using has many through

**URL:** <https://www.ruby-forum.com/t/problem-accessing-item-using-has-many-through/119410>\
**Category:** Rails\
**Created:** [November 6, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/problem-accessing-item-using-has-many-through/119410 "2007-11-06T20:00:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [November 6, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/problem-accessing-item-using-has-many-through/119410/1 "2007-11-06T20:00:24Z")

</div>

My models are

class BakeryOutput \< ActiveRecord::Base  
belongs\_to :recipe  
belongs\_to :customer  
end

class Recipe \< ActiveRecord::Base  
has\_many :ingredient\_recipes  
has\_many :ingredients, :through =\> :ingredient\_recipes  
has\_many :bakery\_outputs  
validates\_presence\_of :name  
validates\_uniqueness\_of :name  
end

class IngredientRecipe \< ActiveRecord::Base  
belongs\_to :ingredient  
belongs\_to :recipe  
end

class Ingredient \< ActiveRecord::Base  
has\_many :ingredient\_recipes  
has\_many :recipes, :through =\> :ingredient\_recipes  
validates\_presence\_of :name  
validates\_uniqueness\_of :name  
end

and in my controller for the show method

def show  
@bakery\_output = BakeryOutput.find(params[:id], :include =\>  
[:recipe, :customer])  
@recipe = Recipe.find(:all, :conditions =\> [“id = ?”,  
@bakery\_output.recipe\_id])  
@ingredients=Recipe.ingredients

```
respond_to do |format|
  format.html # show.rhtml
  format.xml { render :xml => @ingredients.to_xml }
end

```

end

In the controller the first two statement work ok but the third  
@ingredients=Recipe.ingredients  
generates an error

“undefined method `ingredients’ for Recipe:Class”

I’m trying to collect all the ingredients in a recipe

Can someone tell me where I am going wrong, this is my first app.

regards  
Martin

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [November 6, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/problem-accessing-item-using-has-many-through/119410/2 "2007-11-06T20:41:34Z")

</div>

On 6 Nov 2007, at 19:00, Martin E. wrote:

> In the controller the first two statement work ok but the third  
> @ingredients=Recipe.ingredients  
> generates an error
> 
> “undefined method `ingredients’ for Recipe:Class”

It should be @recipe.ingredients

Fred

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [November 6, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/problem-accessing-item-using-has-many-through/119410/3 "2007-11-06T20:46:58Z")

</div>

Frederick C. wrote:

> On 6 Nov 2007, at 19:00, Martin E. wrote:
> 
> > In the controller the first two statement work ok but the third  
> > @ingredients=Recipe.ingredients  
> > generates an error
> > 
> > “undefined method `ingredients’ for Recipe:Class”
> 
> It should be @recipe.ingredients
> 
> Fred

tried that  
@ingredients=@recipe.ingredients

but still get error  
“undefined method `ingredients’”  
I’m using rails 1.2.5 does that make a difference

Martin

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [November 6, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/problem-accessing-item-using-has-many-through/119410/4 "2007-11-06T22:26:46Z")

</div>

Frederick C. wrote:

> On 6 Nov 2007, at 19:46, Martin E. wrote:
> 
> > > Fred
> > 
> > tried that  
> > @ingredients=@recipe.ingredients
> 
> just spotted the other problem: @recipe isn’t a single @recipe as I  
> expected, but an array of recipes.
> 
> Fred

So how do I need to write it please

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [November 6, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/problem-accessing-item-using-has-many-through/119410/5 "2007-11-06T21:57:08Z")

</div>

On 6 Nov 2007, at 19:46, Martin E. wrote:

> > Fred
> 
> tried that  
> @ingredients=@recipe.ingredients

just spotted the other problem: @recipe isn’t a single @recipe as I  
expected, but an array of recipes.

Fred

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [November 6, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/problem-accessing-item-using-has-many-through/119410/6 "2007-11-06T22:29:23Z")

</div>

On 6 Nov 2007, at 21:26, Martin E. wrote:

> > just spotted the other problem: @recipe isn’t a single @recipe as I  
> > expected, but an array of recipes.
> > 
> > Fred
> 
> So how do I need to write it please

```
 @bakery_output = BakeryOutput.find(params[:id], :include =>

```

[:recipe, :customer])  
@recipe = Recipe.find(:all, :conditions =\> [“id = ?”,  
@bakery\_output.recipe\_id])

should be @recipe = @bakery\_output.recipe.

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [November 6, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/problem-accessing-item-using-has-many-through/119410/7 "2007-11-06T22:36:52Z")

</div>

Frederick C. wrote:

> On 6 Nov 2007, at 21:26, Martin E. wrote:
> 
> > > just spotted the other problem: @recipe isn’t a single @recipe as I  
> > > expected, but an array of recipes.
> > > 
> > > Fred
> > 
> > So how do I need to write it please
> 
> ```
> @bakery_output = BakeryOutput.find(params[:id], :include =>
> 
> ```
> 
> [:recipe, :customer])  
> @recipe = Recipe.find(:all, :conditions =\> [“id = ?”,  
> @bakery\_output.recipe\_id])
> 
> should be @recipe = @bakery\_output.recipe.

Thanks a lot, thats fixed it  
I’ve been struggling with that for quite a while  
regards  
Martin
