# To\_xml and helper methods

**URL:** <https://www.ruby-forum.com/t/to-xml-and-helper-methods/152961>\
**Category:** Rails\
**Created:** [November 10, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/to-xml-and-helper-methods/152961 "2008-11-10T16:16:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [November 10, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/to-xml-and-helper-methods/152961/1 "2008-11-10T16:16:29Z")

</div>

Hi,

I have an array with objects and I want to generate an XML like:

 1 result 1 2 result 2 

The helper method generates some urls and it needs as an arguments the  
object.

What would be a proper way to include the result of that method call?

I guess building the xml manually could work, but that means I would  
have to manually enter every object variable.

–  
M.

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [November 11, 2008, 8:51pm UTC](https://www.ruby-forum.com/t/to-xml-and-helper-methods/152961/2 "2008-11-11T20:51:58Z")

</div>

modify the to\_xml method of your object, like so:

class MyObject \< ActiveRecord::Base  
def helper\_method(obj)  
“something-from-obj-#{obj.id}”  
end

def to\_xml  
xml = super  
close\_tag = “\</#{self.class.to\_s.underscore.dasherize}\>”  
xml.gsub(close\_tag, " #{self.helper\_method(self)}\n#{close\_tag}")  
end  
end

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [November 12, 2008, 3:02pm UTC](https://www.ruby-forum.com/t/to-xml-and-helper-methods/152961/3 "2008-11-12T15:02:55Z")

</div>

My problem is that the helper method, is a Helper(app/helpers)  
Method. 🙂

And I can’t use it in the model. Otherwise I could have called to\_xml  
( :method =\> etc).

–  
M.

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [November 12, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/to-xml-and-helper-methods/152961/4 "2008-11-12T15:14:45Z")

</div>

In your model, you should just be able to include the helper:

class MyObject \< ActiveRecord::Base  
include SomeHelper  
end
