Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

No method '_validate_callbacks'

#4876

I have the following code

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

Everytime initialize gets run (via @answer = Answer.new(@survey.survey_questions))

I get the following error:
NoMethodError (undefined method _validate_callbacks' for #<Class:Answer>):<br/> /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:insend' /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:inset_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:invalidates_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:invalidates_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:ineach' /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:ininitialize' /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:inclass_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:inmethod_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:ineach' /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:inmethod_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:ininitialize' /code/survey-app/app/controllers/answers_controller.rb:15:in new' /code/survey-app/app/controllers/answers_controller.rb:15:innew' ...

This worked fine in 3.0.0.beta. Tried looking through the changelogs but can't really tell if anything changed here.

Using Rails 3.0.0.beta4 + Ruby 1.8.7 (tried 1.9.1/2 as well).

Reported by Luke van der Hoeven · June 16th, 2010 @ 09:33 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Luke van der Hoeven
    Luke van der Hoeven
    • Title changed from No method ' to No method '_validate_callbacks'

    June 16th, 2010 @ 10:37 PM

  2. Luke van der Hoeven
    Luke van der Hoeven

    Also, ran a few other setups, I can get around this using 3.0.0.beta under Ruby 1.9.1. All 1.8.7 configurations (including 3.0.0.beta) appear to break.

    June 16th, 2010 @ 10:38 PM

  3. Luke van der Hoeven
    Luke van der Hoeven

    And again, more testing:
    Works with Ruby 1.9.2.

    June 16th, 2010 @ 10:50 PM

  4. José Valim
    José Valim

    Could you add a failing test against Rails ActiveModel test suite? Thanks!

    June 22nd, 2010 @ 04:32 PM

  5. Neeraj Singh
    Neeraj Singh

    @Luke Can you try with the latest code. When I try to recreate your problem on my machine I get following error.

    ArgumentError: Attribute names must be symbols
    

    which is referring to this block

           class_eval {
              attr_accessor name
              validates name, :answer => true
            }
    

    June 22nd, 2010 @ 09:43 PM

  6. Neeraj Singh
    Neeraj Singh
    • State changed from new to incomplete
    • Importance changed from to Low

    @Luke not sure what I did last time.

    This time I not able to proceed because you did not show survey_question model.

    Also I am not sure what is the intent of having class_eval.

    July 16th, 2010 @ 04:05 AM

  7. Luke van der Hoeven
    Luke van der Hoeven

    Turns out it had to do with class reloading underneath the dynamically generated survey code. Had to enable class caching in dev mode. This can be considered resolved, thanks.

    July 16th, 2010 @ 04:42 PM

  8. Neeraj Singh
    Neeraj Singh
    • State changed from incomplete to resolved

    July 16th, 2010 @ 04:43 PM