Forum: Ruby on Rails nested habtm in views

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.
07873906de6f00b0c61537b6464a909e?d=identicon&s=25 John Doe (pathe)
on 2009-05-22 10:44
Hi folks,

hope someone can help me with this:

There are four tables:
Courses (has_many) -> CourseModules (has_and_belongs_to_many) ->
CourseModulesLectures (has_and_belongs_to_many) ->Lectures.

I'd like to get a table with all courses, modules and lectures, which
works quite fine:

<% unless @course.course_modules.empty? %>
<% @course.course_modules.each do |m| %>
  <%=h m.semester %>
  <%=h m.title %>
  <% unless m.lectures.empty? %>
  <% m.lectures.each do |lecture| %>
    <%= link_to lecture.title, lecture %> <!-- -->
  <% end %>
  <% end %>
<% end %>
<% end %>
(stripped unnecessary lines)

The problem is, that 'lecture' in the marked line refers to the id of
the habtm-table 'CourseModulesLectures', and not to the correct id of
the lecture. How can I fix that?

Any ideas?

Thanks a lot!
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2009-05-22 22:53
(Received via mailing list)
Maybe:

  <%= link_to lecture.title, lecture.lecture_id %> <!-- -->

?
07873906de6f00b0c61537b6464a909e?d=identicon&s=25 John Doe (pathe)
on 2009-05-23 08:26
Then the link is set to the courses-controller. It works with something
like link_to xxx, lectures_path(@id, :id => lecture.lecture_id) but
that's not good rails in my eyes. Is there any solution to this?
07873906de6f00b0c61537b6464a909e?d=identicon&s=25 John Doe (pathe)
on 2009-05-25 09:02
Any ideas? Is there any mistake in my design? Should I use has_many
:through?
This topic is locked and can not be replied to.