0

我在每個模型中都有驗證。我也有一個模塊在我的lib文件夾中定義了更新我的模型的方法。我如何獲得我在模型中定義的驗證,以便爲模塊中的方法工作。模塊的ActiveRecord驗證

回答

0

您可以做到這一點:

class MyValidator < ActiveModel::Validator 
    def validate(record) 
    unless MYLIB::isthisgood(record.name) 
     record.errors[:base] << "Name is No Good" 
    end 
    end 
end 

class MyModel < ActiveRecord::Base 
    validates_with MyValidator 
end