A problem with the standard XML Representations

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)

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:

<foo>
  <id>1</id>
  <bar>12</bar>
</foo>

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

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:
<foo>
  <id>/Foos/1.xml</id>
  <bar>/Bars/12.xml</bar>
  <subscriptions>/Foos/1/subscriptions.xml</subscriptions>
  <blogs>/Foos/1/blogs.xml</blogs>
</foo>

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

AdamV wrote:
<snip>

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:
<foo>
  <id>/Foos/1.xml</id>
  <bar>/Bars/12.xml</bar>
  <subscriptions>/Foos/1/subscriptions.xml</subscriptions>
  <blogs>/Foos/1/blogs.xml</blogs>
</foo>

<snip>
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 Fernandez.

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