accepts_nested_attributes_for child association validation failing

后端 未结 6 2050
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 07:02

I\'m using accepts_nested_attributes_for in one of my Rails models, and I want to save the children after creating the parent.

The form works perfectly, but the vali

6条回答
  •  一整个雨季
    2020-12-02 07:41

    Use :inverse_of and validates_presence_of :parent. This should fix your validation problem.

       class Dungeon < ActiveRecord::Base
         has_many :traps, :inverse_of => :dungeon
       end
    
       class Trap < ActiveRecord::Base
         belongs_to :dungeon, :inverse_of => :traps
         validates_presence_of :dungeon
       end
    

    http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_presence_of

    https://github.com/rails/rails/blob/73f2d37505025a446bb5314a090f412d0fceb8ca/activerecord/test/cases/nested_attributes_test.rb

提交回复
热议问题