# To\_xml for Foo::Bar active records

**URL:** <https://www.ruby-forum.com/t/to-xml-for-foo-bar-active-records/138568>\
**Category:** Rails\
**Created:** [May 24, 2008, 9:13pm UTC](https://www.ruby-forum.com/t/to-xml-for-foo-bar-active-records/138568 "2008-05-24T21:13:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [May 24, 2008, 9:13pm UTC](https://www.ruby-forum.com/t/to-xml-for-foo-bar-active-records/138568/1 "2008-05-24T21:13:25Z")

</div>

20 / 20 hind sight, I would probably do things different today but I  
have most of my active record classes under different parent classes.  
For example:

module Foo  
class Bar \< ActiveRecord::Base  
…  
end  
end

The to\_xml for this produces \<foo/bar\> … \</foo/bar\>

The / in a tag name is not allowed. Questions:

1. Is this a bug?
2. If so, suggestion of what to convert it to?

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [May 25, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/to-xml-for-foo-bar-active-records/138568/2 "2008-05-25T18:09:52Z")

</div>

Perry S. wrote:

> module Foo  
> class Bar \< ActiveRecord::Base  
> …  
> end  
> end
> 
> The to\_xml for this produces \<foo/bar\> … \</foo/bar\>
> 
> The / in a tag name is not allowed. Questions:

This is what I did to work around the situation:  
class Hash  
class \<\< self  
private  
def from\_xml\_with\_colon\_fixer(xml)  
fix\_colon\_keys(from\_xml\_without\_colon\_fixer(xml))  
end  
alias\_method\_chain :from\_xml, :colon\_fixer

```
def fix_colon_keys(object)
  case object
  when Hash
    puts "hi"
    object.inject({ }) { |h, (k,v)| h[k.gsub(/ __colon__ /, ":")] = v; 

```

h }  
when Array  
object.map { |e| fix\_colon\_keys(e) }  
else  
object  
end  
end  
end  
end

module ActiveRecord  
class Base  
def to\_xml\_with\_colon\_fixer(options = { }, &block)  
new\_root = (options[:root] || self.class.to\_s).gsub(/:/,  
“ **colon** ”)  
options[:root] = new\_root  
to\_xml\_without\_colon\_fixer(options, &block)  
end  
alias\_method\_chain :to\_xml, :colon\_fixer  
end  
end
