gpt4 book ai didi

ruby-on-rails - ActiveRecord 自动保存 :false doesn't appear to work

转载 作者:行者123 更新时间:2023-12-04 03:35:30 25 4
gpt4 key购买 nike

根据docs ,在关联上设置 :autosave => false 不应在保存父级时保存这些关联。这似乎对我不起作用。我刚刚创建了一个 vanilla Rails 3.0.8 应用程序,这是我得到的:

class Foo < ActiveRecord::Base
has_many :bars, :autosave => false
accepts_nested_attributes_for :bars
end

class Bar < ActiveRecord::Base
belongs_to :foo
end

f = Foo.new :name => 'blah', :bars_attributes => [{:name => 'lah'},{:name => 'lkjd'}]
f.save
f.bars
=> [#<Bar id: 1, name: "lah", foo_id: 1, created_at: "2011-06-20 20:51:02", updated_at: "2011-06-20 20:51:02">, #<Bar id: 2, name: "lkjd", foo_id: 1, created_at: "2011-06-20 20:51:02", updated_at: "2011-06-20 20:51:02">]

什么?为什么要保存 bars

我觉得我正在服用疯狂的药丸!!我错过了什么?

更新:似乎 accepts_nested_attributes_for 会自动保存子项,即使它们不是使用嵌套属性功能构建的。它认为这是一个错误。

最佳答案

这不是错误,而是有意为之。见http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

在方法 accepts_nested_attributes_for 上点击“Source: show”也证明了这一点。

Note that the :autosave option is automatically enabled on every association that accepts_nested_attributes_for is used for.

关于ruby-on-rails - ActiveRecord 自动保存 :false doesn't appear to work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6417399/

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