Master/detail grouping - Advice needed


#1

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.