Validate Before Destroy

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

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

School



        
5条回答
  •  感动是毒
    2020-12-14 00:26

    For Rails 5, returningfalse won't halt the callback chain. You need to use throw(:abort)

    belongs_to :account belongs_to :school

    before_destroy :confirm_presence_of_alternate_administratorship_in_school

    protected

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

提交回复
热议问题