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
--
Posted via http://www.ruby-forum.com/.
Quoting
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...
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.