我有三個班級:學校,帳戶和管理員。破壞前驗證
學校
的has_many:administatorships
的has_many:管理員:通過=>:遺產管理人
帳戶
的has_many:遺產管理人
管理人的職位
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"
end
end
現在,我想發生的事情是,當我打電話destroy
對管理人的職位的實例,爲它的錯誤添加到模型,並防止模型的破壞。我已經刪除了except語句以查看是否阻止了添加錯誤,但事實並非如此。似乎在模型上有錯誤並不能防止發生破壞。
所以我的問題是,有什麼辦法可以防止使用驗證發生破壞?我意識到我可以定義一種只有滿足上述條件才能銷燬的方法,但似乎驗證方法是更優雅的解決方案。
可能重複[我如何驗證'在摧毀在軌道](http:// stackoverflow。問題/ 123078 /我如何做 - 我在軌道上被破壞) – 2014-06-26 18:50:28
不應該是'> 1'嗎?在刪除發生之前,是不是執行了這個查詢? – panzi 2016-02-10 18:09:11
@panzi這是它的統計任何管理員沒有當前管理員的帳戶ID – tanman 2016-02-11 18:22:44