I know this must be easy but I can not see it. I have a table with types of material and a second table of the materials. So; MaterialType has_many :materials and Material belongs_to :material_type I want to display the list of materials as types and include the types that have no materials, to show; Type 1 2 materials defined Material 1 Material 2 <link to add new Type 1 material> Type 2 0 materials defined <link to add new Type 2 material> Type 3 4 materials defined Material 3 Material 4 Material 5 Material 6 <link to add new Type 3 material> Etc The problem is I can show the materials types that have materials defined but I can not work out how to show all the types especially the ones without materials, so I can include the link to add one! Thanks in advance -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 14, 9:31 pm, Philrup <p...-8098yiAR632akBO8gow8eQ@public.gmane.org> wrote:> The problem is I can show the materials types that have materials > defined but I can not work out how to show all the types especially > the ones without materials, so I can include the link to add one! >Isn''t it a case of doing MaterialType.find :all and then iterating over that list ? Fred> Thanks in advance-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 14 July 2010 21:31, Philrup <phil-8098yiAR632akBO8gow8eQ@public.gmane.org> wrote:> I know this must be easy but I can not see it. I have a table with > types of material and a second table of the materials. So; > > MaterialType > has_many :materials > > and > > Material > belongs_to :material_type > > I want to display the list of materials as types and include the types > that have no materials, to show;This might be helpful - http://railscasts.com/episodes/196-nested-model-form-part-1> > Type 1 > 2 materials defined > Material 1 > Material 2 > <link to add new Type 1 material> > Type 2 > 0 materials defined > <link to add new Type 2 material> > Type 3 > 4 materials defined > Material 3 > Material 4 > Material 5 > Material 6 > <link to add new Type 3 material> > Etc > > The problem is I can show the materials types that have materials > defined but I can not work out how to show all the types especially > the ones without materials, so I can include the link to add one!-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sorted it out using interations <% @material_types.each do |mt| %> <h2> <%= mt.name %> </h2> <p>Number of <%= mt.name %> types: <%Material.find(:all, :conditions => {:material_type => mt.material}).count %> </p> <% if Material.find(:all, :conditions => {:material_type => mt.material}).count > 0 %> <table class="info" border="0" cellpadding="5" cellspacing="1"> <tr class="header"> <th>No</th> <th>Name</th> <th>Moisture</th> <th>Dose Size</th> <th>Content</th> <th>MATERIAL EDITOR</th> </tr> <% Material.find(:all, :conditions => {:material_type => mt.material}).each do |mat| %> ..list all the materials It works! But this does feel like RAILS solutions, does anyone know how to put the Material.find stuff in the controller and just call the .each and .count methods? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 July 2010 12:37, Philrup <phil-8098yiAR632akBO8gow8eQ@public.gmane.org> wrote:> Sorted it out using interations > > <% @material_types.each do |mt| %> > <h2> <%= mt.name %> </h2> > <p>Number of <%= mt.name %> types: <%> Material.find(:all, :conditions => {:material_type => > mt.material}).count %> </p> > <% if Material.find(:all, :conditions => {:material_type > => mt.material}).count > 0 %> > <table class="info" border="0" cellpadding="5" > cellspacing="1"> > <tr class="header"> > <th>No</th> <th>Name</th> <th>Moisture</th> > <th>Dose Size</th> <th>Content</th> <th>MATERIAL EDITOR</th> > </tr> > <% Material.find(:all, :conditions => > {:material_type => mt.material}).each do |mat| %> > ..list all the materials > > It works! But this does feel like RAILS solutions, does anyone know > how to put the Material.find stuff in the controller and just call > the .each and .count methods? >Have a look at the rails guide Getting Started at http://guides.rubyonrails.org/ Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.