# Help with controller and view

**URL:** <https://www.ruby-forum.com/t/help-with-controller-and-view/120704>\
**Category:** Rails\
**Created:** [November 19, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/help-with-controller-and-view/120704 "2007-11-19T23:33:21Z")\
**Posts on this page:** 3\
**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 19, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/help-with-controller-and-view/120704/1 "2007-11-19T23:33:21Z")

</div>

I have the following models

class Recipe \< ActiveRecord::Base  
has\_many :ingredient\_recipes  
has\_many :ingredients, :through =\> :ingredient\_recipes  
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  
has\_many :suppliers  
end

class Supplier \< ActiveRecord::Base  
belongs\_to:ingredient  
end

The controller

class TraceabilitiesController \< ApplicationController

def index

```
  if (params[:BakeryOutput].nil? )
     @traceabilities = BakeryOutput.find(:all,:include => [:recipe,

```

:customer])  
else  
@traceabilities = BakeryOutput.find(:all, :conditions =\>  
[“salebatchcode LIKE ?”, “%#{params[:BakeryOutput] [:salebatchcode]}%”],  
:include =\> [:recipe, :customer])  
end

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

```

end

def show  
@bakery\_output = BakeryOutput.find(params[:id], :include =\>  
[:recipe])  
@recipe = @bakery\_output.recipe  
@ingredients=@recipe.ingredients.find(:all)  
respond\_to do |format|  
format.html # show.rhtml  
format.xml { render :xml =\> @traceability.to\_xml }  
end  
end  
end

Views

The Index view show the list of recipes with a link called ingredients  
beside each one. Clicking on the link takes the user to the show method  
of the controller. The show view displays a list of ingredients for that  
recipe. I have got this working so far. I now want to display a list of  
suppliers next to each ingredient like so

Recipe: Cheese and Onion Pie  
Ingredients  
Cheese  
Asda  
Tesco  
Organic Shop  
Onions  
Asda  
Tesco

and so on.  
It is this part that I don’t knowhow to do and am seeking advice  
Thanks  
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 20, 2007, 4:04am UTC](https://www.ruby-forum.com/t/help-with-controller-and-view/120704/2 "2007-11-20T04:04:04Z")

</div>

How about something like this:

## Ingredients

\<%- for ingredient in @ingredients -%\> - \<%= ingredient.name %\> 
 \<%- for supplier in ingredient.suppliers -%\> - \<%= supplier.name %\>
 \<%- end -%\> 
\<%- end -%\>

Of course you will probably want to dry this up a bit using partials  
and include suppliers in your find statement so you are not hitting  
the database for each ingredient.

HTH,  
Nicholas

On Nov 19, 5:33 pm, Martin E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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 20, 2007, 8:58pm UTC](https://www.ruby-forum.com/t/help-with-controller-and-view/120704/3 "2007-11-20T20:58:42Z")

</div>

Thanks Nicholas  
that’s helped a lot  
Martin

Nicholas H. wrote:

> How about something like this:
> 
> ## Ingredients
> 
> \<%- for ingredient in @ingredients -%\> - \<%= ingredient.name %\> 
> \<%- for supplier in ingredient.suppliers -%\> - \<%= supplier.name %\>
> \<%- end -%\> 
> \<%- end -%\>
> 
> Of course you will probably want to dry this up a bit using partials  
> and include suppliers in your find statement so you are not hitting  
> the database for each ingredient.
> 
> HTH,  
> Nicholas
> 
> On Nov 19, 5:33 pm, Martin E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
