Validate number of nested attributes

前端 未结 3 1987
傲寒
傲寒 2021-01-19 14:50

I have a model with nested attributes :

class Foo < ActiveRecord::Base
    has_many :bar
    accepts_nested_attributes_for  :bar
end

It

3条回答
  •  野性不改
    2021-01-19 15:11

    Tony's answer actually won't handle the case where an existing Foo's bars are deleted.

    Since validation of the parent (Foo) happens before the nested children (Bars) are destroyed, Foo will pass validation, then the bars will be destroyed and there will be no errors presented to the user.

    I'd add this as a comment but as of now don't have enough reps

提交回复
热议问题