# To\_xml Hell

**URL:** <https://www.ruby-forum.com/t/to-xml-hell/92759>\
**Category:** Rails\
**Created:** [April 23, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/to-xml-hell/92759 "2007-04-23T22:34:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4da419/32.png) [@Nicholas](https://www.ruby-forum.com/u/Nicholas)\
**Post date:** [April 23, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/to-xml-hell/92759/1 "2007-04-23T22:34:57Z")

</div>

I am trying to create a custom response to a particular action being  
called in my controller and I am getting some really weird errors. I  
am running the rails 1.2 stable branch.

Here is the action in my controller:

# GET /:year/:month/level/1

# GET /:year/:month/level/1.xml

def show  
@level = Level.find(params[:id])  
@assets = @level.assests.find\_by\_year\_month(params[:year],  
params[:month])

respond\_to do |format|  
format.html # show.rhtml  
format.xml { @level.to\_xml :include =\> [@assests] }  
end  
end

Here are the models:  
class Level \< ActiveRecord::Base  
has\_many :assets  
end

class Asset \< ActiveRecord::Base  
belongs\_to :level

def self.find\_by\_year\_month(year, month)  
requested\_date = Date.new(year.to\_i, month.to\_i, 1)  
from = requested\_date - 1  
to = requested\_date \>\> 1  
find\_with\_deleted(:all,  
:conditions =\> ["created\_at \< ? AND ( deleted\_at IS NULL " +  
“OR deleted\_at \< ? AND deleted\_at \> ? )”, to, to, from ])  
end  
end

I have written the find\_by\_year\_month class method on the Asset model  
and it works great. When I run this action I get:

NoMethodError in LevelsController#show

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.macro

C:/…/vendor/rails/activerecord/lib/active\_record/xml\_serialization.rb:  
185:in `add_includes' C:/../vendor/rails/activerecord/lib/active_record/xml_serialization.rb: 180:in `each’  
C:/…/vendor/rails/activerecord/lib/active\_record/xml\_serialization.rb:  
180:in `add_includes' C:/../vendor/rails/activerecord/lib/active_record/xml_serialization.rb: 232:in `to\_s’  
C:/…/vendor/rails/activesupport/lib/active\_support/vendor/builder/  
xmlbase.rb:140:in `call' C:/../vendor/rails/activesupport/lib/active_support/vendor/builder/ xmlbase.rb:140:in `\_nested\_structures’  
C:/…/vendor/rails/activesupport/lib/active\_support/vendor/builder/  
xmlbase.rb:60:in `method_missing' C:/../vendor/rails/activesupport/lib/active_support/vendor/builder/ xmlbase.rb:32:in ` **send** ’  
C:/…/vendor/rails/activesupport/lib/active\_support/vendor/builder/  
xmlbase.rb:32:in `tag!' C:/../vendor/rails/activerecord/lib/active_record/xml_serialization.rb: 230:in `to\_s’  
C:/…/vendor/rails/activerecord/lib/active\_record/xml\_serialization.rb:  
107:in `to_xml' C:/../app/controllers/schedules_controller.rb:23:in `show’  
C:/…/app/controllers/schedules\_controller.rb:21:in `show' C:/../app/controllers/application.rb:32:in `set\_timezone’

Apparently, the error occurs on line 185 of C:/…/vendor/rails/  
activerecord/lib/active\_record/xml\_serialization.rb in `add\_includes’

I can’t figure out why this doesn’t work. According to the post Jamis  
Buck made (  
[http://weblog.jamisbuck.org/2006/3/27/web-services-rails-style](http://weblog.jamisbuck.org/2006/3/27/web-services-rails-style)  
) this should work. Shouldn’t it?

Any help would be greatly appreciated.

Nicholas

---

<div class="post-metadata">

**Author:** ![Nicholas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4da419/32.png) [@Nicholas](https://www.ruby-forum.com/u/Nicholas)\
**Post date:** [April 23, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/to-xml-hell/92759/2 "2007-04-23T22:57:58Z")

</div>

And of course no variable called @assests was set in the controller  
(although I see there is one called @assets). Uninitialized instance  
variables are treated as nil.

---

<div class="post-metadata">

**Author:** ![Nicholas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4da419/32.png) [@Nicholas](https://www.ruby-forum.com/u/Nicholas)\
**Post date:** [April 23, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/to-xml-hell/92759/3 "2007-04-23T22:51:58Z")

</div>

It’s

format.xml { @level.to\_xml :include =\> [:assests] }

Use symbols, not variables. However, given what you are trying to do  
takes  
parameters, you may be best off defining your own to\_xml on Asset as the  
to\_xml document page suggests:

[http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html#M000910](http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html#M000910)

Jason

---

<div class="post-metadata">

**Author:** ![Nicholas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4da419/32.png) [@Nicholas](https://www.ruby-forum.com/u/Nicholas)\
**Post date:** [April 23, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/to-xml-hell/92759/4 "2007-04-23T23:09:00Z")

</div>

That was a type-o on my part. I did check to make sure the variables  
match in my code, but thanks for pointing that out.

- Nicholas

---

<div class="post-metadata">

**Author:** ![Nicholas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4da419/32.png) [@Nicholas](https://www.ruby-forum.com/u/Nicholas)\
**Post date:** [April 23, 2007, 11:13pm UTC](https://www.ruby-forum.com/t/to-xml-hell/92759/5 "2007-04-23T23:13:01Z")

</div>

I have tried the approach you’ve suggested and it does work. I just  
thought you could pass an array in the :include array as Jamis did in  
his post.

def add  
company = params[:person].delete(:company)  
@company = Company.find\_or\_create\_by\_name(company[:name])  
@person = @company.people.create(params[:person])

respond\_to do |wants|  
wants.html { redirect\_to(person\_list\_url) }  
wants.js  
wants.xml { render :xml =\> @person.to\_xml(:include =\> @company) }  
end  
end

The post was made on March 27th of 2006, so it easily could be  
invalid. Any thoughts?

Nicholas
