取消关联时删除直通关联记录



在Rails 6.1应用程序中,我有一个标准的has_many through关联

class Enrollment < ApplicationRecord
belongs_to :topic
belongs_to :job_description, optional: true
end
class Topic < ApplicationRecord
has_many :enrollments
has_many :job_descriptions, through: :enrollments
end
class JobDescription < ApplicationRecord
has_many :enrollments
has_many :topics, through: :enrollments
end

现在,当更新主题(删除工作描述(

@topic.job_descriptions = [...]
@topic.save

具有不再与主题相关联的job_description_idenrollments中的topic_id被设置为NULL。有没有办法删除该行?

尝试更改

has_many :job_descriptions, through: :enrollments

has_many :job_descriptions, through: :enrollments, dependent: :destroy

相关内容

  • 没有找到相关文章

最新更新