Home arrow Tricks, Tips, and Help arrow Code and Documentation for Programmers arrow rails associations deep to xml troubles
rails associations deep to xml troubles Print E-mail
I've been working with rails for a while now and have been trying to figure out how to render complex associated models to_xml for use in my flex frontend without a bunch of messy code. I've set association builders in the model, which ends up breaking down when I need to call a similar association. I've manually generated a hash with lots of loops and for each statements, and tried to use rails built in include and methods statements, and still haven't found a really nice way to make deep nested associations convert to xml well.

Recently I started playing with the collect method and I've settled on this as my best practice method, with the least amount of code. I'm sure it can be improved upon, and I'd gladly hear about a better method. Below is a bit about my models and the controller code to get them into xml format properly.
Here are my model declarations ---

class FindingType < ActiveRecord::Base
   has_many :exam_type_findings, :dependent => :destroy
   has_many :finding_type_measurements, :dependent => :destroy
   has_many :finding_measurements, :through => :finding_type_measurements
   has_many :finding_type_dropdowns, :dependent => :destroy
   has_many :finding_dropdowns, :through => :finding_type_dropdowns
   has_many :finding_type_groups, :dependent => :destroy
   has_many :finding_groups, :through => :finding_type_groups
   has_many :finding_datas
end

class FindingTypeDropdown < ActiveRecord::Base
  belongs_to :finding_type
  belongs_to :finding_dropdown
end

class FindingDropdown < ActiveRecord::Base
  has_many :finding_type_dropdowns
  has_many :finding_types, :through => :finding_type_dropdowns
  has_many :finding_dropdown_values, :dependent => :destroy
  has_many :finding_dropdown_datas
end

class FindingTypeMeasurement < ActiveRecord::Base
   belongs_to :finding_type
   belongs_to :finding_measurement
end

class FindingMeasurement < ActiveRecord::Base
  has_many :finding_measurement_datas
end

class FindingObservation < ActiveRecord::Base
        has_many :finding_group_observations, :dependent => :destroy
        has_many :finding_groups, :through => :finding_group_observations
        has_many :finding_observation_datas
end

class FindingGroup < ActiveRecord::Base
        has_many :finding_type_groups, :dependent => :destroy
        has_many :finding_types, :through => :finding_type_groups
        has_many :finding_group_observations, :dependent => :destroy
        has_many :finding_observations, :through => :finding_group_observations
        has_many :finding_observation_datas
end

class FindingGroupObservation < ActiveRecord::Base
        belongs_to :finding_group
        belongs_to :finding_observation
end


Here is my controller with the code to make the deep nesting into nice xml, I am also including more in the xml, so in the end you would render :xml h.to_xml(:root => "something")

        h['finding_types'] = ExamType.first.finding_types.collect do |ft|

          ft_hash = ft.attributes
          ft_hash['finding_measurements'] = ft.finding_measurements.collect {|fm| fm.attributes}
          ft_hash['finding_dropdowns'] = ft.finding_dropdowns.collect do |fd|
                fd_hash = fd.attributes
                fd_hash['finding_dropdown_values'] = fd.finding_dropdown_values.collect {|fdv| fdv.attributes}
                fd_hash
          end

          ft_hash['finding_groups'] = ft.finding_groups.collect do |fg|
                fg_hash = fg.attributes
                fg_hash['finding_observations'] = fg.finding_observations.collect do |fo|
                        fo_hash = fo.attributes
                        fo_hash['finding_group_id'] = fg.id
                        fo_hash
                end
                fg_hash
          end
          ft_hash
        end

render :xml => h.to_xml(:dasherize => false, :root => "something")


I hope this helps someone out, and if you have a better way, by all means, let me know!

 
< Prev   Next >

Jibwa Work Samples

Contact Jibwa LLC

Under Construction

Jibwa.com is under construction. Watch out for broken links, missing pages, potholes and bulldozers. We apologize for the temporary inconvenience - Jibwa.com Staff

News and Updates

Flex 4 Pediatrics One

Recently Jibwa LCC published demonstration videos and a new website design for Pediatrics One Clinical Management software built on Flex Flash Builder...
Read More ...

Eclectic Flea Simple Business Site

Using hand written materials and some photos we managed to create a simple site for Tucson's artsy thrift store. The Eclectic Flea ...
Read More ...

Flash Builder 2 Release changes from beta

I am moving from (flex) Flash Builder Beta 2 to Flash Builder Release Stable and keeping notes on changes I've had to make to my code. 1.) mx names...
Read More ...

Radiology Gallery

Jibwa and Tripwirearts have built and  launched a new website with Dr Benjamin Strong. radiologygallery.com for radiology continuing medical educ...
Read More ...