ruby-on-rails – ActiveRecord:删除相关记录
发布时间:2020-12-16 21:38:50 所属栏目:百科 来源:网络整理
导读:我得不到的东西…… 我的模型中有这个: class Model ActiveRecord::Base has_many :model_options # a link table for many to many has_many :options,:through = :model_options,:dependent = :destroy,:foreign_key = 'model_id'end 我试着这样做: mode
我得不到的东西……
我的模型中有这个: class Model < ActiveRecord::Base has_many :model_options # a link table for many to many has_many :options,:through => :model_options,:dependent => :destroy,:foreign_key => 'model_id' end 我试着这样做: model = Model.find(id) model.options.delete # also tried model.options.delete_all 但这并不是从数据库中删除记录. model.options.each do |option| option.delete end ……这不是最好的方式. 解决方法model.options.clear Reference (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |