Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在每个模型中都有验证。我还在我的 lib 文件夹中定义了一个模块,其中包含更新我的模型的方法。如何让我在模型中定义的验证适用于我的模块中的方法。
你可以这样做:
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