# Group By - but showing all the groups

**URL:** <https://www.ruby-forum.com/t/group-by-but-showing-all-the-groups/191610>\
**Category:** Rails\
**Created:** [July 15, 2010, 9:27am UTC](https://www.ruby-forum.com/t/group-by-but-showing-all-the-groups/191610 "2010-07-15T09:27:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philrup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f8ce5/32.png) [@Philrup](https://www.ruby-forum.com/u/Philrup)\
**Post date:** [July 15, 2010, 9:27am UTC](https://www.ruby-forum.com/t/group-by-but-showing-all-the-groups/191610/1 "2010-07-15T09:27:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Philrup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f8ce5/32.png) [@Philrup](https://www.ruby-forum.com/u/Philrup)\
**Post date:** [July 15, 2010, 9:30am UTC](https://www.ruby-forum.com/t/group-by-but-showing-all-the-groups/191610/2 "2010-07-15T09:30:37Z")

</div>

On Jul 14, 9:31 pm, Philrup [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Philrup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f8ce5/32.png) [@Philrup](https://www.ruby-forum.com/u/Philrup)\
**Post date:** [July 15, 2010, 9:35am UTC](https://www.ruby-forum.com/t/group-by-but-showing-all-the-groups/191610/3 "2010-07-15T09:35:19Z")

</div>

On 14 July 2010 21:31, Philrup [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 -

> **[\#196 Nested Model Form Part 1 - RailsCasts](http://railscasts.com/episodes/196-nested-model-form-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<div class="post-metadata">

**Author:** ![Philrup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f8ce5/32.png) [@Philrup](https://www.ruby-forum.com/u/Philrup)\
**Post date:** [July 16, 2010, 1:38pm UTC](https://www.ruby-forum.com/t/group-by-but-showing-all-the-groups/191610/4 "2010-07-16T13:38:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Philrup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f8ce5/32.png) [@Philrup](https://www.ruby-forum.com/u/Philrup)\
**Post date:** [July 16, 2010, 4:21pm UTC](https://www.ruby-forum.com/t/group-by-but-showing-all-the-groups/191610/5 "2010-07-16T16:21:20Z")

</div>

On 16 July 2010 12:37, Philrup [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

Have a look at the rails guide Getting Started at

> **[Ruby on Rails Guides](https://guides.rubyonrails.org/)**
>
> Ruby on Rails Guides

Colin
