This problem exists in Rails 2.3.10. Code inspection in
3-0-stable shows the problem also exists there, but I have not
verified this. Below, I am discussing Rails 2.3.10.
ActiveRecord::XmlSerializer#add_associations does not properly
deal with :has_one relationships. It fails to add the type of the
associated data. Worse, if the container object uses STI, the
:has_one associated data will claim to be the type of the container
object.
Consider for example:
class Container < ActiveRecord::Base
has_many :sti_outer_objs
def dump
to_xml({:include=>{:sti_outer_objs=>{:include=>{:sti_inner_obj=>{}}}}})
end
end
class StiOuterObj < ActiveRecord::Base
belongs_to :container
has_one :sti_inner_obj
end
class StiOuterDerivedObj < StiOuterObj
end
class StiInnerObj < ActiveRecord::Base
belongs_to :sti_outer_obj
end
class StiInnerDerivedObj < StiInnerObj
end
Now, use the following hierarchy:
container -> [sti_outer_derived] -> sti_inner_derived
Calling container.dump will show that the generated XML claims
that sti_inner_derived's type is "StiOuterDerived".
The fix is simple and straightforward; we have to calculate the
type of the :has_one data. Attached is a patch against Rails-2.3.10
which accomplishes this and solves the problem.
The patch does not apply against Rails 3.x, but I believe the
modification is trivial.
I am happy to provide any additional information required.