私は非常に一般的なバリデーターを持っており、それに引数を渡したいと思っています。
モデルの例を次に示します。
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
バリデーターが検証しているフィールドに応じて、バリデーターに引数を渡す方法はありますか?
ありがとう