Destroy on blank nested attribute

前端 未结 5 1080
醉酒成梦
醉酒成梦 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条回答
  •  借酒劲吻你
    2020-12-13 10:10

    Similar to Steve Kenworthy's answer, no local variables.

     accepts_nested_attributes_for :tour_dates, :reject_if => :reject_tour, :allow_destroy => true
    
     def reject_tour(attributes)
        if attributes[:when].blank? || attributes[:where].blank?
          if attributes[:id].present?
            attributes.merge!({:_destroy => 1}) && false
          else
            true
          end
        end
      end
    

提交回复
热议问题