Help with controller and view

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
@[email protected](: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

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. [email protected]

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. [email protected]