2011-09-08 80 views
0

我想弄清楚什麼是正確的方法是刪除關聯。但它更容易使用代碼來解釋:選擇一個連接對象,給定關聯,以刪除

class ClassRoom < ActiveRecord::Base 

class ClassJoin < ActiveRecord::Base 
    belongs_to :class_room 
    belongs_to :student 

class Student < ActiveRecord::Base 
    has_many :class_joins 
    has_many :class_rooms, :through => :class_joins 

# show.html.erb 

    <% @student.class_rooms.each do |class_room| %> 
    <%= class_room.name %><br/> 
    <%= link_to "Remove class", student_class_join_path(@student,class_room.something), :method => :delete %> 
    <% end %> 

如何選擇class_join如果我只是在看的has_many:通過關聯?有沒有簡單的方法來選擇它,或者我需要做一個find_by_class_room_id_and_student_id(或類似的東西)。

找到一個類似的question,但我想知道這是否是一個更清潔的方法來做到這一點。

回答

0
<% @student.class_joins.each do |class_join| %> 
    <%= class_join.class_room.name %><br/> 
    <%= link_to "Remove class", student_class_join_path(@student,class_join), :method => :delete %> 
    <% end %> 
+0

是的......我在發佈後想到了這一點。但是...我想要更好的東西。 :) – RyanJM

相關問題