Topic: display nested information correctly
So I'm well on my way with Rails, but every now and then hit walls that I just can't seem to get through. Here's one.
I have three different tables in my App, Issues, Categories, and Stories. This is for a magazine-content tracking app. The first step to linking things up right is that Categories have_many :stories, and that's been fine and works great! (thanks Ryan again for your help).
But a level of complication comes when I want to nest these things inside an issue. Here's where my brain's at on how this should work:
Because categories are the same from issue to issue (more or less), you wouldn't have an issue have_many :categories, because then your categories get caught in a single issue and you have to repeat yourself each issue. Bad UI. So instead, issues should have_many :stories. And yeah, that works great and I've hooked up the story form to allow assigning issues in there. Wonderful.
However, in an issue view, I want to see what categories are in an issue and what stories are inside those categories. The simple look at this is:
Now I've been able to set up the CATEGORY view to list exactly as described w/o the issue qualifier like so:
<% for category in @categories %>
<b><%= h(category.section) %></b><br />
<% for story in category.stories %>
<%= link_to story.title, :controller => "story_admin", :action => "show", :id => story.id %><br />
<% end %>
Voila, works like a charm. BUT, I want this to work in the issue view instead.
Now, I can list stories in an issue no problem, by reproducing the code above, but swapping the issue calls where it's currently calling category.
And I can list the categories assigned to specific stories by calling story.category.section instead of story.title.
But what I can't figure out is now to list a category once, then have the stories assigned to it nest below, then list the next cat.... like in the above example.
Any help would be much apprecaited.