Nested attributes not working creating children with new parent

允我心安 提交于 2019-12-04 17:57:25

The thing with validating presence of parent like this is timing !! actually the Shift is not yet saved so when trying to create nested ranges it won't find parent Shift in database.

I found this workaround here

class Shift < ActiveRecord::Base
  attr_accessible :ranges_attributes
  has_many :ranges, :inverse_of => :shift
  accepts_nested_attributes_for :ranges, allow_destroy: true
end

and i quote (with minor modifications) from the same source:

With this option rails won't try to get parent from database when child is validated. The parent will be got from memory. If you don't familiar with this option I strongly recommend you to read an official rails guide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!