Destroy on blank nested attribute

前端 未结 5 1068
醉酒成梦
醉酒成梦 2020-12-13 09:36

I would like to destroy a nested model if its attributes are blanked out in the form for the parent model - however, it appears that the ActiveRecord::Callbacks

5条回答
  •  -上瘾入骨i
    2020-12-13 09:55

    With your current code it's not possible, because of the reject_if option passed to accepts_nested_attributes_for.

    As Christ Mohr said, the easiest way is to set the _destroy attribute for the nested model when updating the parent, and the nested model will be destroyed. Refer to the docs for more info on this, or this railscast.

    Or you can use a gem like cocoon, or awesome_nested_fields.

    To do specifically what you want, you should remove the reject_if option, and handle the logic in a callback inside the parent object. It should check for blank values in the tour_dates_attributes and destroy the nested model. But tread carefully...

提交回复
热议问题