Search code examples
rubyarraysattributessavon

How do I use savon nested array attributes! hash?


I am using Ruby Savon and i have to create a request that contains an array that has attributes in it. My problem is how do i add an attribute in each of the child array.

  <persons>
    <person id=1>
      <firstName>JOHN</firstName>                  
    </person>
    <person id=2>
      <firstName>ANNIE</firstName>
    </person>
  </persons>

I tried putting the attributes to an array without success as well.

{
  persons: {
    person:  [ { firstName: "JOHN"}, { firstName: "ANNIE" } ], 
    :attributes! => { :person=> [ { id: "1" }, { id: "2" } ] }
  }
}.to_soap_xml

Solution

  • {
         persons: {
            person:  [ { firstName: "JOHN", :@id => 1}, { firstName: "ANNIE", :@id => 2 } ]
         }
    }