# Generating XML for a model

**URL:** <https://www.ruby-forum.com/t/generating-xml-for-a-model/141524>\
**Category:** Rails\
**Created:** [June 26, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/generating-xml-for-a-model/141524 "2008-06-26T18:46:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [June 26, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/generating-xml-for-a-model/141524/1 "2008-06-26T18:46:26Z")

</div>

Hi yet again!

I’m currently using an erb template to generate XML for a model (say  
/persons.xml), because I need custom tags added to the XML I have to  
generate it from zero.

I tried using person.to\_xml but that returns a whole XML document.

Is there an easier way to insert custom values in a model’s XML  
generation? I could redefine the to\_xml method but it’d be the same work  
of having to include all the other values.

Could I define kind of like a virtual column for the model that gets  
picked up by to\_xml?

Thanks!

- Ivan V.

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [June 26, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/generating-xml-for-a-model/141524/2 "2008-06-26T18:54:28Z")

</div>

On 26 Jun 2008, at 17:46, Ivan V. wrote:

> generation? I could redefine the to\_xml method but it’d be the same  
> work  
> of having to include all the other values.
> 
> Could I define kind of like a virtual column for the model that gets  
> picked up by to\_xml?

yes. See the :only, :except and :methods options to to\_xml, or the  
block form of to\_xml

Fred

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [June 26, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/generating-xml-for-a-model/141524/3 "2008-06-26T19:00:00Z")

</div>

Frederick C. wrote:

> On 26 Jun 2008, at 17:46, Ivan V. wrote:
> 
> > generation? I could redefine the to\_xml method but it’d be the same  
> > work  
> > of having to include all the other values.
> > 
> > Could I define kind of like a virtual column for the model that gets  
> > picked up by to\_xml?
> 
> yes. See the :only, :except and :methods options to to\_xml, or the  
> block form of to\_xml
> 
> Fred

Duh! I should’ve look into the to\_xml documentation.

Thanks a lot.

Ivan V.
