删除 has_and_belongs_to_many 表中的记录 - Rails

Are*_*rel 0 ruby-on-rails has-and-belongs-to-many

我一直在寻找,但找不到关于如何删除 HABTM 表中记录的好答案。我想很多人都有同样的要求。

简单地说,我有学生、班级和班级_学生

我希望学生能够放弃课程,或删除已为该学生注册该课程的 HABTM 记录。

对此必须有一个简单的答案。有谁知道它是什么?

Ven*_*ice 5

.destroy 或 .delete 在这种情况下不起作用的原因是由于中间表中缺少主键。然而,我们的父对象有一个非常酷的方法,叫做 {other_obj}_ids。它是右表对象的左表对象上的 id 集合。这些信息当然是从我们的中间表中填充的。

因此,考虑到这一点,我们有 2 个对象类(Student 和 Classes)。如果你不做任何花哨的事情,活动记录魔术通常可以找出中间表,但建议使用 has_many :through。

class Student < ActiveRecord::Base
    has_and_belongs_to_many :classes
end

class Classes < ActiveRecord::Base
    has_and_belongs_to_many :students
end
Run Code Online (Sandbox Code Playgroud)

我们现在可以使用此设置在中间表方面做什么......

student = Student.find_by(1)
student.classes # List of class objects this student currently has. 
student.class_ids # array of class object ids this student currently has

# how to remove a course from the middle table pragmatically
course = Course.find_by({:name => 'Math 101'})

# if this is actually a real course...
unless course.nil? 
   # check to see if the student actually has the course...
   if student.class_ids.include?(course.id)
       # update the list of ids in the array. This triggers a database update
       student.class_ids = student.class_ids - [course.id]
   end
end
Run Code Online (Sandbox Code Playgroud)

我知道回答这个问题有点晚了,但我今晚刚刚经历了这个确切的情况,并想在这里分享解决方案。

现在,如果您希望通过表单删除它,因为您现在可以看到它是如何以务实的方式处理的,只需确保表单输入是嵌套的,以便它具有以下效果: