# A problem with the standard XML Representations

**URL:** <https://www.ruby-forum.com/t/a-problem-with-the-standard-xml-representations/138361>\
**Category:** Rails\
**Created:** [May 22, 2008, 10:22am UTC](https://www.ruby-forum.com/t/a-problem-with-the-standard-xml-representations/138361 "2008-05-22T10:22:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdamV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@AdamV](https://www.ruby-forum.com/u/AdamV)\
**Post date:** [May 22, 2008, 10:22am UTC](https://www.ruby-forum.com/t/a-problem-with-the-standard-xml-representations/138361/1 "2008-05-22T10:22:54Z")

</div>

Hey all,

There is one thing that has been bothering me about how Rails  
implements REST, particularly in the XML form. I’ll be the first to  
admit that I haven’t read all of Roy’s dissertation but I think I have  
the gist of it from his presentation “The Rest of REST” (http://  
[roy.gbiv.com/talks/200709\_fielding\_rest.pdf](http://roy.gbiv.com/talks/200709_fielding_rest.pdf))

One of the key things that I get from him is that its URLs that make  
connections between representations, and its the presence of URLs that  
allows us to create adaptive behaviour. So now I wonder why it is that  
if I have a model that belongs to another:

class Foo \< ActiveRecord::Base  
belongs\_to :bar  
end

I get something like the following XML from /Foos/1.xml:

 1 12

As far as I can tell, this is not RESTful, or at least not as RESTful  
as:  
  
/Foos/1.xml  
/Bars/12.xml

further, if we add a has\_many and has\_many :through to our model, such  
as:  
class Foo \< ActiveRecord::Base  
belongs\_to :bar  
has\_many :subscriptions  
has\_many :blogs, :through =\> :subscriptions  
end

I would expect to see:  
  
/Foos/1.xml  
/Bars/12.xml  
/Foos/1/subscriptions.xml  
/Foos/1/blogs.xml

Since Rails doesn’t do this natively, how can I abuse it to make it do  
my will?

---

<div class="post-metadata">

**Author:** ![AdamV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@AdamV](https://www.ruby-forum.com/u/AdamV)\
**Post date:** [May 22, 2008, 10:48am UTC](https://www.ruby-forum.com/t/a-problem-with-the-standard-xml-representations/138361/2 "2008-05-22T10:48:12Z")

</div>

AdamV wrote:

> /Foos/1.xml  
> /Bars/12.xml  
> /Foos/1/subscriptions.xml  
> /Foos/1/blogs.xml  
>   
>   
> Hi Adam

I am afraid that I can only help you with the second issue (above):

The ActiveRecord.to\_xml method takes parameters that allow you to  
include associated objects, namely:

render :xml =\> @foo.to\_xml( :includes =\> [:subscription, :blogs] )

I have yet to implement this myself (although I will be in the next few  
days) - but I got the above from the very excellent “The Rails Way” by  
Obie F…

The Rails Way has a good section on xml processing, so it may very well  
have a solution to your first problem as well, but I cannot remember  
anything offhand. I would also be interested in a solution to the first  
problem as well.

R
