【问题标题】:has_one belongs_to association autosave => true not savinghas_one belongs_to 关联 autosave => true 不保存
【发布时间】:2011-03-23 09:51:00
【问题描述】:

我有两个模型

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

Pref

belongs_to :board

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

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

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

有没有办法在保存板时自动保存首选项对象?

提前谢谢你

【问题讨论】:

    标签: ruby-on-rails autosave


    【解决方案1】:

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

    如果为真,则始终保存关联的 如果标记为对象或销毁它 销毁,保存父母时 目的。如果为 false,则永远不要保存或 销毁关联的对象。

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

    类似的东西:

    after_create :create_pref
    
    def create_pref
      pref.create!
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多