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:

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?

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