Reputation: 7622
class A
include ActiveModel::Validations
attr_reader :operator
def initialize(operator)
@operator = operator
validates_inclusion_of :operator, in => operators
end
def operators
....
end
end
Here I want to validate the operator for inclusion_of dynamically. the method operators returns an array of operators which is dynamic.
The above code is not working. How can I implement the validation dynamically? r
Upvotes: 4
Views: 5759
Reputation: 9764
You may try:
def initialize(operator)
self.class.class_eval do
validates_inclusion_of :operator, :in => operators
end
end
although I don't understand why can't you just define it at the class level. Note that argument to :in can be a lambda, for details refer to: http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_inclusion_of
Upvotes: 5