ruby-on-rails - 如何解决 []= 删除中间关系栏
问题描述
我将通过下一个示例模拟我的问题,假设我有student
可能与 s 相关的course
s,并且course
包含多个s student
。因此,学生和课程之间是多对多的关系,考虑以下代码:
# student.rb
class Student < ApplicationRecord
has_many :student_courses
has_many :courses, through: :student_courses
end
# student_course.rb
class StudentCourse < ApplicationRecord
belong_to :student
belong_to :course
end
# course.rb
class Course < ApplicationRecord
has_many :student_courses
has_many :students, through: :student_courses
end
以上所有内容都可以,但是当我试图在这些关系之间进行删除并在中间表中进行回调时
> c = Course.first
> c.students = [] # this perform a deletion correctly but can't be triggered by a call back
makestudents
和空数组正确删除了关系,但它不是由回调触发的。使用StudentCenter.where(something).destroy_all
时会触发after_destroy
等。
我的问题是,我怎么能使用= []
并保持触发,因为我发现中间桌子上的操作在阅读代码时有点吵。提前致谢。
解决方案
推荐阅读
- sql - 如何在microsoft access中根据复合键过滤表单数据
- javascript - 使用由父 Document 对象创建的元素创建 iframe 内容
- javascript - 如何在时间限制后启用 JavaScript 按钮单击功能?
- sqlite - SQFlite - 通过连接抛出删除行:接近 LEFT 语法错误
- java - 通过 WebSphere 部署 Kotlin Spring Boot 应用程序
- angular - 如何获取和更新用户属性 AWS amplify angular 6
- php - 在功能 PHP Wordpress 的菜单项上按类别循环帖子
- sql - Powershell foreach 在多行上插入 INSERT INTO SQL Server DB
- angular - Nswag 生成 get 方法的重复版本
- pandas - Python Pandas:LabelEncoding 拟合未知变量