Home arrow Tricks, Tips, and Help arrow Advanced Tips arrow Rails viewing nested attributes with xml
Rails viewing nested attributes with xml Print E-mail
I've run into this problem many times with rails. I need to list out something and inside of this list may be other nested lists of properties.  I'm using two models for this example

CustomTask has_many StateChanges
StateChange belongs_to CustomTask


You would think a simple CustomTask.all.to_xml(:methods => ['state_changes']) would give you a nice list with nested state_changes, but unfortunately to_xml seems to not want to serialize this array properly. Also, forcing the :include in the find does nothing to help the to_xml either.

I've done this on multiple nested models and it always converts to xml much better.

  def list
        custom_tasks = CustomTask.find(:all).collect do |ct|
                h = ct.attributes
                h['state_changes'] = ct.state_changes
                h
        end

        render :xml => custom_tasks.to_xml(:dasherize => false)

  end

If there is a simpler way, by all means, let us know and we'll post it here!

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