gpt4 book ai didi

ruby-on-rails - has_one belongs_to association autosave => true 不保存

转载 作者:行者123 更新时间:2023-12-04 06:45:02 25 4
gpt4 key购买 nike

我有两个模型

Board
has_one :pref, :autosave => true, :dependent => :destroy

Pref

belongs_to :board

pref 对象具有在数据库中设置的默认值,因此在创建板时不需要使用任何信息来创建对象。电路板的 ID 在 pref 表中。

因为 :autosave=> true 我认为当我创建并保存一个新的 Board 对象时,会自动创建并保存一个 pref 对象。

这不是这样工作的,所以我一定是误会了。

有没有办法在保存板时自动保存 pref 对象?

提前致谢

最佳答案

autosave => true 不应为您创建元素。 docs say :

If true, always save the associated object or destroy it if marked for destruction, when saving the parent object. If false, never save or destroy the associated object.

你可以使用 callback在创建新的 board 时创建 pref 对象。

类似的东西:

after_create :create_pref

def create_pref
pref.create!
end

关于ruby-on-rails - has_one belongs_to association autosave => true 不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5403372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com