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 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" ?> - <simulation> <created_at type="datetime">2008-09-10T22:15:28Z</created_at> <desc>Standard Server Simulation</desc> <id type="integer">4</id> <name>GroceryStore</name> <run_length type="float">8.0</run_length> <time_units>hr</time_units> <updated_at type="datetime">2008-09-10T22:15:28Z</updated_at> <user_id type="integer">3</user_id> + <buffers type="array"> + <buffer.../> </buffers> + <resources type="array"> + <resource.../> </resources> + <loads type="array"> + <load.../> </loads> - <distributions type="array"> + <distribution.../> + <distribution.../> </distributions> </simulation>
But what I really want is for the distribution that belongs to the <load> element to be nested inside that element, and the same thing for the distribution that belongs to the <resource> 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