有些物体拒绝被破坏(铁轨)

发布于 2024-12-10 20:37:11 字数 340 浏览 1 评论 0原文

我正在运行一个 rake 任务来销毁所有“课程”对象:

task :destroy_all_classes => :environment do 
  Course.all.each do |c| 
    c.destroy
  end 
end

这会销毁大部分课程,但还剩下 12 个课程(总共 40 个以上)。什么可能阻止我删除它们?

如果我尝试通过控制台手动删除每门课程,我会得到以下信息:

ruby-1.9.2-p290 :030 > Course.find(1).destroy
 => false 

I'm running a rake task to destroy all of my "course" objects:

task :destroy_all_classes => :environment do 
  Course.all.each do |c| 
    c.destroy
  end 
end

This destroys most of the courses, but 12 remain (out of 40+). What might be preventing me from deleting them?

If I try deleting each course manually, through the console, I get this:

ruby-1.9.2-p290 :030 > Course.find(1).destroy
 => false 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-12-17 20:37:11

启用级联销毁。

当然与另一个模型至少有一个“有很多”或“有一个”关系。像

class Course < ActiveRecord::Base
  has_many :somethings
  has one : something
end

make it

class Course < ActiveRecord::Base
  has_many :somethings, :dependent => :destroy
  has one :something, :dependent => :destroy
end

这样的东西你也可以使用

 accepts_nested_attributes_for :something, :allow_destroy => true

祝你好运!

Enable cascade destroy.

Course have at lest one "has many"or "has one" relation with another model.Something like

class Course < ActiveRecord::Base
  has_many :somethings
  has one : something
end

make it

class Course < ActiveRecord::Base
  has_many :somethings, :dependent => :destroy
  has one :something, :dependent => :destroy
end

You can also use

 accepts_nested_attributes_for :something, :allow_destroy => true

Best of luck !!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文