如何始终将嵌套项目保存在Rails应用程序中(即使它们没有被更改)?

问题描述 投票:0回答:1

在我的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会有所作为,但不幸的是没有。

ruby-on-rails ruby activerecord nested-forms
1个回答
0
投票
您可以在Project上进行回调:

class Project < ApplicationRecord before_save :touch_tasks def touch_tasks tasks.all.touch_all end end

https://apidock.com/rails/ActiveRecord/Relation/touch_all
© www.soinside.com 2019 - 2024. All rights reserved.