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

标签 ruby-on-rails activerecord autosave

根据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/

相关文章:

ruby-on-rails - ActiveRecord 验证中属性的旧值和新值

macos - Cocoa - 添加菜单以支持版本? (就像在文本编辑中一样)

ruby-on-rails - Rescue_from用于javascript请求

ruby-on-rails - Redis 订阅 + Rails ActionController::Live 挂起

ruby-on-rails - 指定要从 Rails 查询结果中排除的字段

ruby-on-rails - PostgreSQL、Rails 和 :order => problem

angular - 如何在 Angular 4 中实现自动保存 react 形式?

Emacs 自动保存 : Why are files not stored in the correct folder?

ruby-on-rails - 角色未添加到数据库中的用户

ruby-on-rails - 如何在 Ubuntu 中连接 PostgreSQL