bra*_*rad 3 activerecord autosave ruby-on-rails
根据文档,: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">]
Run Code Online (Sandbox Code Playgroud)
什么??它为什么要保存bars?
我觉得我正在服用疯狂的药!我错过了什么?
更新:看起来好像accepts_nested_attributes_for自动保存子项,即使它们不是使用嵌套属性功能构建的.它认为这是一个错误.
这不是错误,而是意图.看到http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
单击方法accepts_nested_attributes_for上的"Source:show"也证明了这一点.
请注意:自动保存选项会在每个使用accepts_nested_attributes_for的关联上自动启用.