Undefined method "updated?"

I recently updated Rails to the latests version, and one of my existing
apps broke. More speficially, it can’t find the method “updated?” when
it is attempting to call it on one of my models.

The stack trace follows:

NoMethodError
undefined method `updated?’ for #MyModel:0xc91d50

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:in
method_missing' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:352:incallback’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:346:in
callback' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:341:ineach’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:341:in
callback' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:252:increate_or_update’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1392:in
save_without_validation' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:736:insave_without_transactions’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in
save' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:intransaction’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:91:in
transaction' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:118:intransaction’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in
save' #{RAILS_ROOT}/app/controllers/my_controller.rb:104:increate’

I’m having a hard time finding out why this is happening and where I
should start investigating the matter. I tried looking through the
ActiveRecord code, but I can’t locate where the “updated?” call is even
coming from.

Any help is appreciated.

Thanks,
-Szymon R.

Szymon R. wrote:

should start investigating the matter. I tried looking through the
ActiveRecord code, but I can’t locate where the “updated?” call is even
coming from.

This problem has cropped up several times in the list, but no-one
has yet posted an exact cause. It’s related to the name you’ve
chosen to call a belongs_to association, perhaps conflicting with
something else in your model.

Check out these threads:

http://lists.rubyonrails.org/pipermail/rails/2005-December/007657.html
http://lists.rubyonrails.org/pipermail/rails/2006-March/025508.html
http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/a3aa66f313e715a8/f23679b884f5c549

Please post any resolution.


We develop, watch us RoR, in numbers too big to ignore.

Wow. So this took me a while to track down. I was about to quit and
revert back to an old version of rails.

My model had two associations with a table called customers. I had a
customer_id and a contact_id field.

I also had a statement:
validates_presence_of :customer, :contact
in my model.

After updating to Rails 1.1.6 from 1.0, whenever I created a new model
or saved it, I got this exception. Given that the semantics of my model
creation imply that the record is created, saved and then passed on for
the user to edit, I concluded that it was enough to validate only on an
update. This solved the issue of receving the error while
creating/saving a new record. But I still could not save the record.
The reason for this was that my update method got the contact_id and
customer_id and saved the integers in those fields. Apparently, this
isn’t enough for Rails to validate the contact and customer
associations. I simply switched the code from:

self.customer_id = params[:cust_id]
self.contact_id = params[:contact_id]

to:

self.customer = Customer.find(params[:cust_id])
self.contact = Customer.find(params[:contact_id])

Hopefully, my solution will help anyone else who runs into this problem
as well.

-Szymon

Szymon R. wrote:

I recently updated Rails to the latests version, and one of my existing
apps broke. More speficially, it can’t find the method “updated?” when
it is attempting to call it on one of my models.

The stack trace follows:

NoMethodError
undefined method `updated?’ for #MyModel:0xc91d50

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:in
method_missing' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:352:incallback’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:346:in
callback' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:341:ineach’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:341:in
callback' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/callbacks.rb:252:increate_or_update’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1392:in
save_without_validation' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:736:insave_without_transactions’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in
save' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:intransaction’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:91:in
transaction' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:118:intransaction’
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:126:in
save' #{RAILS_ROOT}/app/controllers/my_controller.rb:104:increate’

I’m having a hard time finding out why this is happening and where I
should start investigating the matter. I tried looking through the
ActiveRecord code, but I can’t locate where the “updated?” call is even
coming from.

Any help is appreciated.

Thanks,
-Szymon R.

i have face the same question yet
i find what the problem is
the special variable
for exemple

you use then
"belongs_to :user "
in the first

if you use “@user” the last , the problem will call
so , don’t use the special variable

Szymon R. wrote:

Wow. So this took me a while to track down. I was about to quit and
revert back to an old version of rails.

My model had two associations with a table called customers. I had a
customer_id and a contact_id field.

I also had a statement:
validates_presence_of :customer, :contact
in my model.

After updating to Rails 1.1.6 from 1.0, whenever I created a new model
or saved it, I got this exception. Given that the semantics of my model
creation imply that the record is created, saved and then passed on for
the user to edit, I concluded that it was enough to validate only on an
update. This solved the issue of receving the error while
creating/saving a new record. But I still could not save the record.
The reason for this was that my update method got the contact_id and
customer_id and saved the integers in those fields. Apparently, this
isn’t enough for Rails to validate the contact and customer
associations. I simply switched the code from:

self.customer_id = params[:cust_id]
self.contact_id = params[:contact_id]

to:

self.customer = Customer.find(params[:cust_id])
self.contact = Customer.find(params[:contact_id])

Hopefully, my solution will help anyone else who runs into this problem
as well.

-Szymon

After struggling for an hour or two yesterday, I figured this out! In a
model, whenever you use something like:

class ContactInformation < ActiveRecord::Base
belongs_to :customer

Any ContactInformation instance will automatically create within it a
“customer” instance. That’s why you can use:
ContactInformation.find(:first).customer

GREAT. The problem is if you for some reason overwrite that “customer”
instance variable, like in this case:

class ContactInformation < ActiveRecord::Base
belongs_to :customer

def do_something
@customer = Customer.find(:first)
end
end

In this case, we’ve overwritten the “customer” that was automatically
created by the “belongs_to” relationship! And this new customer that
we’ve created doesn’t have the method “updated?” on it; the customer
created by the belongs_to would have had it.

I hope that helps someone else out!

The solution is never use instance variable names with the name of the
association.