相关疑难解决方法(0)

在rails中销毁之前检查所有关联

我的应用程序中有一个重要的模型,有许多关联.如果我想检查before_destroy回调中的所有引用,我必须执行以下操作:

has_many :models_1
has_many :models_2
mas_many :models_3
....
....
has_many :models_n

before_destroy :ensure_not_referenced

def :ensure_not_referenced
   if models_1.empty? and models_2.empty? and models_3.empty? and ... and models_n.empty?
       return true
   else
       return false
       errors.add(:base,'Error message')
   end
end
Run Code Online (Sandbox Code Playgroud)

问题是,有没有办法立即执行所有验证?感谢名单!

ruby-on-rails associations

8
推荐指数
1
解决办法
6447
查看次数

标签 统计

associations ×1

ruby-on-rails ×1