# Custom to\_xml

**URL:** <https://www.ruby-forum.com/t/custom-to-xml/117963>\
**Category:** Rails\
**Created:** [October 22, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/custom-to-xml/117963 "2007-10-22T21:07:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nfessel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a4c791/32.png) [@nfessel](https://www.ruby-forum.com/u/nfessel)\
**Post date:** [October 22, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/custom-to-xml/117963/1 "2007-10-22T21:07:04Z")

</div>

I have two models: ledger and label. A ledger can contain many  
labels.

Here is the xml returned from [http://localhost/ledgers/78.xml:](http://localhost/ledgers/78.xml:)

 11 30.0 2007-10-22 2007-10-22 Deposit 78

Question:

How can I return the labels associated with the ledger? The xml should  
look like this (notice the “labels” addition):

 11 30.0 2007-10-22 2007-10-22 Deposit 78 Food Music

---

<div class="post-metadata">

**Author:** ![nfessel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a4c791/32.png) [@nfessel](https://www.ruby-forum.com/u/nfessel)\
**Post date:** [October 22, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/custom-to-xml/117963/2 "2007-10-22T21:12:21Z")

</div>

I just figured it out.

Instead of this: format.xml { render :xml =\> @ledger.to\_xml }

do this: format.xml { render :xml =\> @ledger.to\_xml(:include =\>  
[:labels]) }

Works like a charm. 🙂

---

<div class="post-metadata">

**Author:** ![nfessel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a4c791/32.png) [@nfessel](https://www.ruby-forum.com/u/nfessel)\
**Post date:** [October 22, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/custom-to-xml/117963/3 "2007-10-22T21:14:41Z")

</div>

FYI I found the answer here:  
[http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html#M000910](http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html#M000910)
