ActiveRecord自动保存:false似乎不能工作

时间:2023-01-28 09:28:52

According to the docs, setting :autosave => false on an association should NOT save those associations when you save the parent. This doesn't appear to work for me. I just created a vanilla Rails 3.0.8 app and here's what I get:

根据文档,在保存父关联时,关联上的设置:autosave => false不应该保存这些关联。这似乎对我不起作用。我刚刚创建了一个香草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">]

What?? Why did it save the bars?

什么? ?它为什么要拯救那些栅栏?

I feel like I'm taking crazy pills!! What am I missing?

我觉得我在吃疯狂的药丸!!我缺少什么?

Update: It appears as if accepts_nested_attributes_for automatically saves children, even if they're not built using the nested attributes feature. It think this is a bug.

更新:它显示为accepts_nested_attributes_for自动保存子节点,即使它们不是使用嵌套属性特性构建的。它认为这是一个bug。

2 个解决方案

#1


5  

This is not a bug, instead it is intended. see http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

这不是错误,而是故意的。参见http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

A click on "Source: show" on method accepts_nested_attributes_for also proves this.

单击方法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.

注意:在每个使用accepts_nested_attributes_for的关联上,自动启用:autosave选项。

#2


0  

Adding to Innerpeacer response, it doesn't make sense setting the autosave attribute to false if you are using accepts_nested_attributes_for. One of the reasons to use accepts_nested_attributes_for is to save children and parent at the same time.

添加到Innerpeacer响应,如果使用accepts_nested_attributes_for,那么将autosave属性设置为false是没有意义的。使用accepts_nested_attributes_for的原因之一是同时保存子节点和父节点。

What you can do is:

你能做的是:

f = Foo.new :name => 'blah'
f.save
f.bars_attributes = [{:name => 'lah'},{:name => 'lkjd'}]

or

f = Foo.new :name => 'blah'
f.save
f.bars = [Bars.new({:name => 'lah'}), Bars.new({:name => 'lkjd'})]

#1


5  

This is not a bug, instead it is intended. see http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

这不是错误,而是故意的。参见http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

A click on "Source: show" on method accepts_nested_attributes_for also proves this.

单击方法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.

注意:在每个使用accepts_nested_attributes_for的关联上,自动启用:autosave选项。

#2


0  

Adding to Innerpeacer response, it doesn't make sense setting the autosave attribute to false if you are using accepts_nested_attributes_for. One of the reasons to use accepts_nested_attributes_for is to save children and parent at the same time.

添加到Innerpeacer响应,如果使用accepts_nested_attributes_for,那么将autosave属性设置为false是没有意义的。使用accepts_nested_attributes_for的原因之一是同时保存子节点和父节点。

What you can do is:

你能做的是:

f = Foo.new :name => 'blah'
f.save
f.bars_attributes = [{:name => 'lah'},{:name => 'lkjd'}]

or

f = Foo.new :name => 'blah'
f.save
f.bars = [Bars.new({:name => 'lah'}), Bars.new({:name => 'lkjd'})]