Rails can't render polymorphic associations to XML?

Posted by ambertch on Stack Overflow See other posts from Stack Overflow or by ambertch
Published on 2010-04-24T11:17:38Z Indexed on 2010/04/24 11:23 UTC
Read the original article Hit count: 345

Filed under:

When I render XML with an :include clause for a polymorphic association I have, it doesn't work. I end up with the XML returning the object pointers instead of the actual objects, like:

<posts>
   #<Comment:0x102ed1540>#<Comment:0x102ecaa38>#<Comment:0x102ec7fe0>#<Comment:0x102ec3cd8>
</posts>

Yet as_json works! When I render JSON with :include clause, the associations are rendered correctly and I get something like:

posts":[
{"type":"Comment","created_at":"2010-04-20T23:02:30-07:00","id":7,"content":"fourth comment"},
{"type":"Comment","created_at":"2010-04-20T23:02:26-07:00","id":6,"content":"third comment"}]

My current workaround is using XML builder, but I'm not too comfortable with that in the long run. Does anyone happen to know about this issue? I'm kind of in a catch-22 because while XML doesn't render the associations, as_json doesn't render in a kosher json format (returns an array rather than a list of hashes as proper json should) and the deserializer I'm using on the client side would require modification to parse the json correctly.

© Stack Overflow or respective owner

Related posts about ruby-on-rails