Validate Before Destroy

后端 未结 5 1117
忘掉有多难
忘掉有多难 2020-12-13 23:49

I have three classes: School, Account, and Administratorship.

School



        
5条回答
  •  盖世英雄少女心
    2020-12-14 00:32

    I ended up using code from here to create a can_destroy override on activerecord: https://gist.github.com/andhapp/1761098

    class ActiveRecord::Base
      def can_destroy?
        self.class.reflect_on_all_associations.all? do |assoc|
          assoc.options[:dependent] != :restrict || (assoc.macro == :has_one && self.send(assoc.name).nil?) || (assoc.macro == :has_many && self.send(assoc.name).empty?)
        end
      end
    end
    

    This has the added benefit of making it trivial to hide/show a delete button on the ui

提交回复
热议问题