# Nested XML using REST

**URL:** <https://www.ruby-forum.com/t/nested-xml-using-rest/226025>\
**Category:** Rails\
**Created:** [September 12, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/nested-xml-using-rest/226025 "2008-09-12T23:57:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rainyglade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rainyglade](https://www.ruby-forum.com/u/rainyglade)\
**Post date:** [September 12, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/nested-xml-using-rest/226025/1 "2008-09-12T23:57:59Z")

</div>

My Simulation model looks like this:  
class Simulation \< ActiveRecord::Base  
belongs\_to :user  
has\_many :resources  
has\_many :loads  
has\_many :distributions  
has\_many :buffers…

My Load model:  
class Load \< ActiveRecord::Base  
has\_many :load\_dist\_links  
has\_many :distributions, :through =\> :load\_dist\_links  
belongs\_to :simulation…

My Resource model:  
class Resource \< ActiveRecord::Base  
has\_many :rsc\_q\_links  
has\_many :rsc\_dist\_links  
has\_many :distributions, :through=\> :rsc\_dist\_links  
has\_many :buffers, :through =\> :rsc\_q\_links  
belongs\_to :simulation…

[http://localhost:3000/simulations/4.xml](http://localhost:3000/simulations/4.xml) uses this show method:

def show  
#@simulation = Simulation.find(params[:id])  
@simulation = current\_user.simulations.find(params[:id])  
@resources = @simulation.resources(force\_reload = true)  
@distributions = @simulation.distributions(force\_reload = true)  
@buffers = @simulation.buffers(force\_reload = true)  
@loads = @simulation.loads(force\_reload = true)

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @simulation.to_xml(:include=> [

```

:buffers, :resources, :loads, :distributions])}  
end  
rescue ActiveRecord::RecordNotFound =\> e  
prevent\_access(e)  
end

which gives me the following XML:

 \<?xml version="1.0" encoding="UTF-8" ?\>
- 2008-09-10T22:15:28Z Standard Server Simulation 4 GroceryStore 8.0 hr 2008-09-10T22:15:28Z 3

- 
- \<buffer…/\>  
- 
- \<resource…/\>  
- 
- \<load…/\>  

- 

- \<distribution…/\>
- \<distribution…/\>  
  

But what I really want is for the distribution that belongs to the  
 element to be nested inside that element, and the same thing for  
the distribution that belongs to the element.

When I call /simulations/4/resources/2.xml or  
/simulations/4/loads/1.xml, I get what I want because I can use :include  
on the to\_xml statement. But everything I’ve tried in the  
simulations\_controller show method, changing the simulation model, even  
messing with routing, gives me errors.

For example, I have tried

def show  
#@simulation = Simulation.find(params[:id])  
@simulation = current\_user.simulations.find(params[:id])  
@loads = @simulation.loads(force\_reload = true)  
@resources = @simulation.resources(force\_reload = true)  
@load\_distributions = @loads.distributions(force\_reload = true)  
@rsc\_distributions = @resources.distributions(force\_reload = true)  
@buffers = @simulation.buffers(force\_reload = true)

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @simulation.to_xml(:include=> [

```

:buffers, :resources, :loads, :load\_distributions, rsc\_distributions])}  
end  
rescue ActiveRecord::RecordNotFound =\> e  
prevent\_access(e)  
end

but the error is "undefined method “distributions” for the line  
@load\_distributions=@loads.distributions(force\_reload = true).

How can I modify the simulations\_controller#show method so that I get  
the distributions associated with loads and/or resources to be properly  
nested?

LG
