Group By - but showing all the groups

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

Type 2 0 materials defined Type 3 4 materials defined Material 3 Material 4 Material 5 Material 6 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

On Jul 14, 9:31 pm, Philrup [email protected] 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

On 14 July 2010 21:31, Philrup [email protected] wrote:

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

Sorted it out using interations

<% @material_types.each do |mt| %>

<%= mt.name %>


Number of <%= mt.name %> types: <%=
Material.find(:all, :conditions => {:material_type =>
mt.material}).count %>


<% if Material.find(:all, :conditions => {:material_type
=> mt.material}).count > 0 %>


<% 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?

No Name Moisture Dose Size Content MATERIAL EDITOR

On 16 July 2010 12:37, Philrup [email protected] wrote:

cellspacing=“1”>
the .each and .count methods?

Have a look at the rails guide Getting Started at
http://guides.rubyonrails.org/

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs