Validate Before Destroy

In case somebody stumbles here looking for Rails 5, returning false is not the way anymore. Use throw(:abort) instead, like in Martin Cabrera Diaubalick's answer.


Before Rails 5

If you return false from that before_destroy method, it will prevent the destruction.


This is a Rails 5 answer, if you return false it will give a deprecation warning: "Returning false in Active Record and Active Model callbacks will not implicitly halt a callback chain in Rails 5.1".

def confirm_presence_of_alternate_administratorship_in_school
  return if school.administrators.count(["administratorships.account_id != #{id}"]) > 0
  errors[:base] << 'The school must have at least one administrator'
  throw :abort
end