# Master/detail grouping - Advice needed

**URL:** <https://www.ruby-forum.com/t/master-detail-grouping-advice-needed/110814>\
**Category:** Rails\
**Created:** [July 31, 2007, 9:27am UTC](https://www.ruby-forum.com/t/master-detail-grouping-advice-needed/110814 "2007-07-31T09:27:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![axplains](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@axplains](https://www.ruby-forum.com/u/axplains)\
**Post date:** [July 31, 2007, 9:27am UTC](https://www.ruby-forum.com/t/master-detail-grouping-advice-needed/110814/1 "2007-07-31T09:27:57Z")

</div>

Hello, I need help on a this problem:

I have a “Categories / Items” database in a “has\_many - belongs\_to”  
relationship

On the “Items” page I want to display the items grouped by Category,  
so in the controller I have:

class ItemController \< ApplicationController

…

def list

@categories = Category.find(:all, :order =\> “description”)  
@items = Item.find(:all, :order =\> “description”)

end  
…

in the Items “list” view, I have:

\<%# cycles though the categories %\>  
\<%= render :partial =\> “category”, :collection =\> @categories } %\>

The “\_category.rhtml” partial is:

\<%= category.description %\>  
\<%# I have to populate the @items collection here, by category %\>  
\<% @items = Items.find( :all, :conditions =\> [“category\_id LIKE ?” ,  
category.id] ) %\>  
\<%

# and render the items lines for the category

# the “\_item.rhtml” partial contains the data for the item row %\>

\<%= render :partial =\> “item”, :collection =\> @items %\>

I would like to do this WITHOUT populating the “@items” collection  
inside the view, and moving all the data operations in the controller if  
possible.

Someone got any ideas for “refactoring” this?  
Thank you in advance.
