Validate Before Destroy

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

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

School



        
5条回答
  •  孤城傲影
    2020-12-14 00:42

    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
    

提交回复
热议问题