Wednesday, October 8, 2008

Undefined method "updated?"

I was juggling around to find out the cause. I kept on getting error:

NoMethodError (undefined method `updated?' for #): /vendor/rails/activerecord/lib/active_record/attribute_methods.rb:251:in `method_missing' /vendor/rails/activerecord/lib/active_record/associations.rb:907:in `belongs_to_before_save_for_person' /vendor/rails/activesupport/lib/active_support/callbacks.rb:173:in `send' /vendor/rails/activesupport/lib/active_support/callbacks.rb:173:in `evaluate_method' /vendor/rails/activesupport/lib/active_support/callbacks.rb:161:in `call' /vendor/rails/activesupport/lib/active_support/callbacks.rb:93:in `run' /vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `each' /vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `send' /vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `run' /vendor/rails/activesupport/lib/active_support/callbacks.rb:272:in `run_callbacks' /vendor/rails/activerecord/lib/active_record/callbacks.rb:298:in `callback' /vendor/rails/activerecord/lib/active_record/callbacks.rb:206:in `create_or_update' /vendor/rails/activerecord/lib/active_record/base.rb:2200:in `save_without_validation' /vendor/rails/activerecord/lib/active_record/validations.rb:901:in `save_without_dirty' /vendor/rails/activerecord/lib/active_record/dirty.rb:75:in `save_without_transactions'

I have 2 models Person and Rate with

class Person
  has_many :rates
end

class Rate
  belongs_to :person
end

On create/update of person or rate, I kept on getting the error. After deep digging I found the cause of the issue.

In my Rate model, I was creating a @person object. The association (belongs_to, has_many etc..) calls before_save callback for associated model. Since I created a new @person object in my Rate class, it was calling 'updated?' for new object and hence NoMethodError.

I renamed the @person object and resolved the issue.

Hope this helps.

1 comment:

David Michael said...
This comment has been removed by the author.