katsuo
November 16, 2007, 11:22am
1
Hello,
I would like to list all the chapter description related to a particuler
title on catalog/list.rhtml. There are two associated tables ‘Title’ and
‘Chapter’. I have written the following but does not work. Can anyone
assist me on this issue?
#Title table
book_name :string
chapter_id :integer
#Chapter table
description :string
class Title < ActiveRecord::Base
has_many :chapters
class Chapter < ActiveRecord::Base
belongs_to :title
class CatalogController < ApplicationController
def list
@title = Title.find(params[id])
end
list_rhtml
<% for title in @titles %>
<%= link_to title.book_name, :action => 'show', :id=> title
%>
<%= title.chapter.description %>
<% end %>
katsuo
November 16, 2007, 11:38am
2
if title has_many chapters, then chapters need a title_id, not titles a
chapter_id
if you correct this, then in your view this should do the trick:
<% @titles.each do |title| %>
<%= link_to title.book_name, :action => 'show', :id=> title
%>
<% title.chapters.each do |chapter| %>
<%= chapter.description %>
<% end %>
<% end %>
katsuo
November 18, 2007, 6:42am
3
Thorsten M. wrote:
if title has_many chapters, then chapters need a title_id, not titles a
chapter_id
if you correct this, then in your view this should do the trick:
<% @titles.each do |title| %>
<%= link_to title.book_name, :action => 'show', :id=> title
%>
<% title.chapters.each do |chapter| %>
<%= chapter.description %>
<% end %>
<% end %>
Thank you very very much. With your assistance, it came out the way I
wanted.