给定关联,选择一个连接对象,以便删除

时间:2011-09-08 18:01:53

标签: ruby-on-rails ruby-on-rails-3 activerecord

我正在试图找出删除关联的正确方法。但用代码解释更容易:

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 %>

如果我只是查看has_many:through关联,如何选择class_join?有没有一种简单的方法可以选择它,或者我是否需要执行find_by_class_room_id_and_student_id(或类似的东西)。

找到了类似的question,但我想知道这些是否更简洁。

1 个答案:

答案 0 :(得分: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 %>