在我的Rails 6应用程序中,我具有此关联:
class Project < ApplicationRecord
has_many :tasks, :dependent => :destroy, :autosave => true
accepts_nested_attributes_for :tasks, :allow_destroy => true
end
现在,当我将project
及其嵌套的tasks
保存在一起时,仅当任务中至少一个属性发生更改时,才更新任务。
出于特定于我的应用程序/用例的原因,我希望tasks
到[[always在数据库中进行更新,尽管(即使它们根本没有改变!),当我单击保存时也是如此。
我希望添加:autosave => true
会有所作为,但不幸的是没有。
class Project < ApplicationRecord
before_save :touch_tasks
def touch_tasks
tasks.all.touch_all
end
end
https://apidock.com/rails/ActiveRecord/Relation/touch_all