# Confused about to\_xml() in ActiveRecord::Base subclass

**URL:** <https://www.ruby-forum.com/t/confused-about-to-xml-in-activerecord-base-subclass/157347>\
**Category:** Rails\
**Created:** [January 9, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/confused-about-to-xml-in-activerecord-base-subclass/157347 "2009-01-09T17:43:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [January 9, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/confused-about-to-xml-in-activerecord-base-subclass/157347/1 "2009-01-09T17:43:03Z")

</div>

Since all my efforts to control how to\_xml() is formatting dates has  
failed, I’m now considering writing my own to\_xml(). However, from  
the limited examples I’ve found, I just don’t understand how to  
actually reference the columns for the records in my record set.

Here’s a simplified view of my ActiveRecord object:

class Errors \< ActiveRecord::Base  
set\_table\_name “legacy\_errors”

# legacy\_errors has three columns: id, msg, error\_date

# Since the default to\_xml is generating error\_dates in the format

“Mon Nov 03 00:00:00 -0600 2008”,

# and I cannot seem to force a different behavior, I want to

create my own to\_xml.

# Examples online show the following as how to override to\_xml:

```
def to_xml(options = {})
  options[:indent] ||= 2
  xml = options[:builder] ||= Builder::XmlMarkup.new(:indent =>

```

options[:indent])  
xml.instruct! unless options[:skip\_instruct]  
# v v v  
xml.level\_one do  
xml.tag!(:second\_level, ‘content’)  
end  
# ^ ^ ^  
end  
end

The code between the v and ^ markers is where the body of each XML  
object is generated. If I were generating by hand, I would do  
something like this:  
errors.each do |err|  
s \<\< “” \<\< err.id \<\< “”  
s \<\< “” \<\< err.msg \<\< “”  
s \<\< “\<error\_date\>” \<\< err.error\_date.strftime("%Y-%m-%d") \<\< “\</  
error\_date\>”  
end

I’m not at all understanding how to do my thing within the v ^ section  
of to\_xml().

Any help is appreciated!

Thanks.

---

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [January 9, 2009, 6:21pm UTC](https://www.ruby-forum.com/t/confused-about-to-xml-in-activerecord-base-subclass/157347/2 "2009-01-09T18:21:15Z")

</div>

On Jan 9, 2009, at 11:42 AM, michael\_teter wrote:

> # legacy\_errors has three columns: id, msg, error\_date
> 
> ```
> xml.instruct! unless options[:skip_instruct]
> 
> ```
> 
> something like this:  
> Any help is appreciated!
> 
> Thanks.

that should be the encoding of a single Errors instance.

xml.error do |x|  
x.id(self.id)  
x.msg(self.msg)  
x.error\_date(self.error\_date.strftime(‘%Y-%m-%d’))  
end

I think that you’d get a bunch of errors with:

Errors.find(:all, :conditions =\> […]).to\_xml(:root =\> ‘errors’)

By default, I get output of attributes like this:  
2009-01-09T02:58:31Z  
110  
From Anne  
When I do #to\_xml. Notice the date format for the type=“datetime”.  
What is the actual class of error\_date? Is it a Date or a DateTime or  
a Time?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [January 9, 2009, 7:01pm UTC](https://www.ruby-forum.com/t/confused-about-to-xml-in-activerecord-base-subclass/157347/3 "2009-01-09T19:01:44Z")

</div>

Rob,

Thanks much for the good response.

To answer your question about the data type, it’s “date” in Oracle.  
When I use the default to\_xml() (without suppressing types and the  
dasherizer) it looks like this:  
\<error-date type="ActiveRecord::ConnectionAdapters::OracleColumn  
"\>Wed Nov 12 00:00:00 -0600 2008

In fact, every element is listed as “OracleColumn”. I did try the  
following to control my date presentation:  
proc = Proc.new {|options| options[:builder].tag!  
(‘error\_date\_x’, options[:object].error\_date.strftime(“%Y-%m-%d”))}  
s = errs.to\_xml(:dasherize =\> false, :skip\_types =\> true, :procs  
=\> [proc])  
That just blows up.  
NoMethodError: You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.error\_date

What’s really odd is I’ve been using the default to\_xml() for months  
and receiving “YYYY-MM-DD” as my format. Now suddenly I’m getting a  
different format. Perhaps I updated gems and got something new…

So now, if I cannot simply control the formatting of that one column,  
I’ll do my own to\_xml() (following your example).

On Jan 9, 11:20 am, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
