accepts_nested_attributes_for child association validation failing

后端 未结 6 2059
隐瞒了意图╮
隐瞒了意图╮ 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 08:03

    Use this answer for Rails 2, otherwise see below for the :inverse_of answer

    You can work around this by not checking for the project_id if the associated project is valid.

    
    class Task < ActiveRecord::Base
      belongs_to :project
    
      validates_presence_of :project_id, :unless => lambda {|task| task.project.try(:valid?)}
      validates_associated :project
    end
    

提交回复
热议问题