在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_id
的enrollments
中的topic_id
被设置为NULL
。有没有办法删除该行?
尝试更改
has_many :job_descriptions, through: :enrollments
至
has_many :job_descriptions, through: :enrollments, dependent: :destroy