I’m using ROXML to read and automatically build Ruby objects form properly formatted XML.
I’ve got nested resources, so within my XML I have, for example:
<blog ... >
<post ... />
<post ... />
<post ... />
</blog>
Here’s the problem:
- When I read this url, I get back a
blogobject, upon which I can callblog.posts.countand I get back3, as expected – that’s working. - When I call
blog.firstI get back the firstpostin thepostscollection, as defined by ROXML – that’s working. - However, what isn’t working is calling the parent object. I’d like to be able to take a given
postobject and callpost.blogto get its parentblogobject back. How can I define this relationship on thepostobject? Do I simply define a method, and provide anxml_accessordeclaration? This doesn’t seem right (and hasn’t worked), becausexml_accessormethods are meant to pull attributes directly from XML, and within thepostsection of the XML, there will be noblogXML – it’s the other way around.
So, how do I access parent objects from a child object, from objects constructed via ROXML?
I’m more or less the current author/maintainer of ROXML (http://github.com/Empact).
First I wonder, did you submit this: https://github.com/Empact/roxml/issues/36 That would be the current issue tracking this problem.
In any case, the answer is the the library doesn’t current build up this relationship, and if you think of it, it’s not that crazy that we don’t, after all ActiveRecord doesn’t build both sides of a db relationship without symmetric has_many/belongs_to declarations.
But ROXML can! After all the sub-objects are parsed as children under the higher-level object, we could just propagate down the parents info to establish the connection.
I don’t have time at the moment to do this work, but if you’d like to make the addition to the library, I’m more than happy to vet and include it in the library.
You might also look at Representable: https://github.com/apotonick/representable it was created in part from the ROXML codebase and is more actively maintained. Not sure if it has this feature.