Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/6406
Created by Christopher Thompson - 2011-02-10 18:05:01 UTC
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
class StiOuterObj < ActiveRecord::Base
class StiOuterDerivedObj < StiOuterObj
class StiInnerObj < ActiveRecord::Base
class StiInnerDerivedObj < StiInnerObj
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.
Attachments saved to Gist: http://gist.github.com/971774