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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
How about something like this: <h2>Ingredients</h2> <ul> <%- for ingredient in @ingredients -%> <li><%= ingredient.name %> <ul> <%- for supplier in ingredient.suppliers -%> <li><%= supplier.name %></li> <%- end -%> </ul> </li> <%- end -%> </ul> 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 Evans <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > @ingredien...-JFuMWr9U1RW59qKN6rKkt41CJt1vBjcI@public.gmane.org(: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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Nicholas that''s helped a lot Martin Nicholas Henry wrote:> How about something like this: > > <h2>Ingredients</h2> > <ul> > <%- for ingredient in @ingredients -%> > <li><%= ingredient.name %> > <ul> > <%- for supplier in ingredient.suppliers -%> > <li><%= supplier.name %></li> > <%- end -%> > </ul> > </li> > <%- end -%> > </ul> > > 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 Evans <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---