我有一个非常通用的验证器,我想向它传递参数。
这是一个示例模型:
class User
include Mongoid::Document
field :order_type
has_many :orders, inverse_of :user
validates: orders, generic: true #i want to pass argument (order_type)
field :task_type
has_many :tasks, inverse_of :user
validates: tasks, generic: true #i want to pass argument (task_type)
end
和示例验证器:
class GenericValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
if some_validation?(object)
object.errors[attribute] << (options[:message] || "is not formatted properly")
end
end
end
有没有办法根据验证的字段将参数传递给验证器?
谢谢