我有一个如下所示的「耙子任务」:
desc "Cleanup Snippets with Empty Diffs"
task cleanup_snippets_with_empty_diffs: :environment do
Snippet.includes(:diffs).where(diffs: { body: "<div class=\"diff\"></div>"}).destroy_all
end
然而当我运行它时,我得到了这个:
$ rake cleanup_snippets_with_empty_diffs
rake aborted!
ActiveRecord::ReadOnlyRecord: Diff is marked as readonly
这可能是什么原因引起的?
编辑1
请注意,我的Snippet.rb
模型如下:
class Snippet < ApplicationRecord
has_many :diffs, dependent: :destroy
end
还有像这样的Diff.rb
:
class Diff < ApplicationRecord
belongs_to :snippet
end
includes
,而不是joins
。 - marcamillionjoins
方法。请查看我的更新答案以获取进一步解释和可能的解决方案。谢谢。 - Bart Jedrocha