# XML into database

**URL:** <https://www.ruby-forum.com/t/xml-into-database/135275>\
**Category:** Rails\
**Created:** [April 20, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/xml-into-database/135275 "2008-04-20T14:04:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [April 20, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/xml-into-database/135275/1 "2008-04-20T14:04:49Z")

</div>

Hi,

I like to save www-forms into one text-field in database by using XML.

Example:

Bill Gates ....

And not to make 100\*n fields into table.

Is there any easy way for this? Add/Edit/Show data…?

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [April 20, 2008, 2:50pm UTC](https://www.ruby-forum.com/t/xml-into-database/135275/2 "2008-04-20T14:50:21Z")

</div>

> I like to save www-forms into one text-field in database by using XML.
> 
> And not to make 100\*n fields into table.

Hi,

you could define the virtual (=not mapped to db fields) attributes in  
your model through attr\_accessor. Then you could use the after\_find  
and before\_save callbacks to write the contents of these attributes as  
XML into your single text field, as well as deserialize them back into  
the virtual attributes when a model object is loaded from database.

Jan

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [April 20, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/xml-into-database/135275/3 "2008-04-20T18:59:12Z")

</div>

On 20 Apr 2008, at 13:04, Gi Ga wrote:

> …
> 
> And not to make 100\*n fields into table.

You do realize that is really quite limiting? For example you cannot  
efficiently pick the first 10 users ordered by last name, select those  
users with property x and so on. You will also have to provide  
accessors for all the attributes, although I imagine it would not be  
hard to generate those at run time (similar to what ActiveRecord does).

Fred
