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