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!
|