Forum: Ruby on Rails Master/detail grouping - Advice needed

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7487e7b1d4faa10ac80722d55da9e8a8?d=identicon&s=25 Ax Plains (axplains)
on 2007-07-31 09:27
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.
This topic is locked and can not be replied to.