How do I 'validate' on destroy in rails

前端 未结 11 1911
你的背包
你的背包 2020-11-28 06:33

On destruction of a restful resource, I want to guarantee a few things before I allow a destroy operation to continue? Basically, I want the ability to stop the destroy oper

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 07:13

    I have these classes or models

    class Enterprise < AR::Base
       has_many :products
       before_destroy :enterprise_with_products?
    
       private
    
       def empresas_with_portafolios?
          self.portafolios.empty?  
       end
    end
    
    class Product < AR::Base
       belongs_to :enterprises
    end
    

    Now when you delete an enterprise this process validates if there are products associated with enterprises Note: You have to write this in the top of the class in order to validate it first.

提交回复
热议问题