耙任务摧毁记录,但不破坏关联

时间:2014-04-05 05:15:28

标签: ruby-on-rails-3 activerecord model-associations

我有一个rake任务来销毁超过特定时间的模型的记录。即使记录被删除,但关联也没有。

rake任务很简单:

Product.where("marked = ? AND created_at < ?", false, 15.days.ago).destroy_all

协会:

产品型号:

has_many :features, :dependent => :destroy

功能模型:

belongs_to :product

1 个答案:

答案 0 :(得分:1)

试试:

@products_to_destroy = Product.where("marked = ? AND created_at < ?", false, 15.days.ago)

@products_to_destroy.destroy_all

OR

Product.destroy_all("marked = ? AND created_at < ?", false, 15.days.ago)

参考:http://apidock.com/rails/ActiveRecord/Base/destroy_all/class

希望有所帮助:)