加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读