# Undefined method \`updated?' when saving object

**URL:** <https://www.ruby-forum.com/t/undefined-method-updated-when-saving-object/72111>\
**Category:** Rails\
**Created:** [October 14, 2006, 9:42am UTC](https://www.ruby-forum.com/t/undefined-method-updated-when-saving-object/72111 "2006-10-14T09:42:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajasaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rajasaur](https://www.ruby-forum.com/u/rajasaur)\
**Post date:** [October 14, 2006, 9:42am UTC](https://www.ruby-forum.com/t/undefined-method-updated-when-saving-object/72111/1 "2006-10-14T09:42:29Z")

</div>

I have a Doctor - Secretary relation with  
class Doctor \< Person  
has\_many :secretaries  
…  
end

and  
class Secretary \< Person  
belongs\_to :doctor, :foreign\_key =\> ‘doctor\_id’  
…  
end

When saving a Secretary object after assigning a doctor to it, I get a  
stacktrace like  
undefined method `updated?' for #<Doctor:0xb785bdc0> /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1792:in`method\_missing’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/callbacks.rb:342:in  
`callback' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbacks.rb:335:in`callback’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/callbacks.rb:330:in  
`callback' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbacks.rb:248:in`create\_or\_update’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/base.rb:1392:in  
`save_without_validation' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:724:in`save\_without\_transactions’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/transactions.rb:126:in  
`save’

Digging into active\_record/association.rb’s belongs\_to method, it  
indicates that there is a before\_save method that does a  
association = instance\_variable\_get("@#{reflection.name}")  
if !association.nil?  
if association.new\_record?  
association.save(true)  
end

if association.updated?  
…  
end  
end

It is the “updated?” method here that its failing. If I put in a dummy  
updated method in the Doctor class, something like  
def updated?  
true  
end

then all is well, and my association is saved correctly. Reading through  
the manuals, it indicates that saving from the Doctor side(which has the  
has\_many) will automatically persist the Secretaries, but in this case,  
I want my Secretary to be saved along with the Doctor associated.

Is this a bug? The updated? method has been defined in the  
belongs\_to\_association.rb and belongs\_to\_polymorphic\_association.rb, but  
i think it needs to be defined in the has\_many\_association.rb as well.

Im a rails newbie so if all of the above is wrong, please correct me.

Thanks  
Raja

---

<div class="post-metadata">

**Author:** ![rajasaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rajasaur](https://www.ruby-forum.com/u/rajasaur)\
**Post date:** [October 14, 2006, 9:43am UTC](https://www.ruby-forum.com/t/undefined-method-updated-when-saving-object/72111/2 "2006-10-14T09:43:33Z")

</div>

FWIW I found 2 other posts with a similar problem but no answers  
[http://www.forbiddenweb.org/viewtopic.php?id=85572](http://www.forbiddenweb.org/viewtopic.php?id=85572)  
and  
[http://article.gmane.org/gmane.comp.lang.ruby.rails/37392](http://article.gmane.org/gmane.comp.lang.ruby.rails/37392)

---

<div class="post-metadata">

**Author:** ![rajasaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rajasaur](https://www.ruby-forum.com/u/rajasaur)\
**Post date:** [October 14, 2006, 11:47am UTC](https://www.ruby-forum.com/t/undefined-method-updated-when-saving-object/72111/3 "2006-10-14T11:47:44Z")

</div>

Can you post or check your validation methods in both Secretary and  
Doctor?

Quoting  
[http://lists.rubyonrails.org/pipermail/rails/2006-March/026246.html:](http://lists.rubyonrails.org/pipermail/rails/2006-March/026246.html:)  
“my problem was caused by a badly chosen name of a variable in the  
validates method of my interface model.”

Looks like this might be the cause…

---

<div class="post-metadata">

**Author:** ![rajasaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rajasaur](https://www.ruby-forum.com/u/rajasaur)\
**Post date:** [October 14, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/undefined-method-updated-when-saving-object/72111/4 "2006-10-14T17:40:52Z")

</div>

Eden Li wrote:

> Can you post or check your validation methods in both Secretary and  
> Doctor?
> 
> Quoting  
> [http://lists.rubyonrails.org/pipermail/rails/2006-March/026246.html:](http://lists.rubyonrails.org/pipermail/rails/2006-March/026246.html:)  
> “my problem was caused by a badly chosen name of a variable in the  
> validates method of my interface model.”

Thanks so much. That was just the reason. I created a local variable in  
the Secretary class called doctor and did a  
doctor.secretaries \<\< self

That seems to be the problem. Renaming that local variable to something  
like doctor\_for\_secretary seems to have fixed it.

Looking back at the reason, it would never get into the block that I  
mentioned in my original post if the :belongs\_to object is null and if i  
have a different variable name, that would be the case and the  
association is only saved due to the link from the Doctor’s side.
