I keep getting an error from the ActiveModel:EachValidator module when I try to use it as follows:
class AnswerValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || "question not answered") if value.nil? and !record.errors[attribute].include?(options[:message] || "question not answered")
end
end
class Answer
include ActiveModel::Validations
attr_accessor :id
def to_param
(id = self.id) ? id.to_s : nil
end
def to_key
persisted? ? nil : [id]
end
#answer will always be new
def persisted?
false
end
def set_attributes(values)
values.each do |k,v|
self.send "#{k}=".to_sym, v
end
end
def initialize(questions=nil)
# create accessor methods
if questions
questions.each do |q|
name = q["sqt_name"].gsub(/(-|\s)/, "_").downcase.to_sym
class_eval {
attr_accessor name
validates name, :answer => true if q['sqt_required_fl']
}
end
end
end
end
This is doing some custom dynamic form generation stuff, but the validator fails with the error:
NoMethodError (undefined method `_validate_callbacks' for #<Class:Answer>):
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-3.0.0.beta4/lib/active_support/core_ext/class/inheritable_attributes.rb:172:in `_validate_callbacks'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:580:in `_update_validate_superclass_callbacks'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:435:in `send'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:435:in `__update_callbacks'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:474:in `set_callback'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activemodel-3.0.0.beta4/lib/active_model/validations.rb:128:in `validate'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activemodel-3.0.0.beta4/lib/active_model/validations/with.rb:82:in `validates_with'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activemodel-3.0.0.beta4/lib/active_model/validations/with.rb:70:in `each'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activemodel-3.0.0.beta4/lib/active_model/validations/with.rb:70:in `validates_with'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activemodel-3.0.0.beta4/lib/active_model/validations/validates.rb:88:in `validates'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activemodel-3.0.0.beta4/lib/active_model/validations/validates.rb:81:in `each'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activemodel-3.0.0.beta4/lib/active_model/validations/validates.rb:81:in `validates'
/code/survey-app/app/models/answer.rb:46:in `initialize'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-3.0.0.beta4/lib/active_support/core_ext/kernel/singleton_class.rb:11:in `class_eval'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activesupport-3.0.0.beta4/lib/active_support/core_ext/kernel/singleton_class.rb:11:in `class_eval'
/code/survey-app/app/models/answer.rb:44:in `initialize'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_collection.rb:408:in `method_missing'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_proxy.rb:214:in `method_missing'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_proxy.rb:214:in `each'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_proxy.rb:214:in `send'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_proxy.rb:214:in `method_missing'
/home/server/.rvm/gems/ruby-1.8.7-p249/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_collection.rb:408:in `method_missing'
/code/survey-app/app/models/answer.rb:41:in `initialize'
/code/survey-app/app/controllers/answers_controller.rb:15:in `new'
/code/survey-app/app/controllers/answers_controller.rb:15:in `new'
...
If I remove line validates name, :answer => true if q['sqt_required_fl'] it doesn't complain. But otherwise, I can't figure out how to get around this error.
Ruby 1.8.7 Rails 3.0.0.beta4
Thanks for any help or insight on this.