Pls help noob with to_xml

my beginner’s luck has vaporized on this one. new to rails; new to
ruby; but been around the block several times.

my model: (Category.rb)

class Category < ActiveRecord::Base
self.to_xml(:only => [:id, :name], :include => [:entries] )
has_many :entries
end

without the to_xml mod, everything works fine. With this line in place,
I get the following error:

undefined method ‘to_xml’ for class:0xb76bef9c

Application Trace:
/usr/lib/ruby/gems/activerecord-2.0.1/lib/active_record/base.rb:1459: in
‘method_missing’

Please help!!!
should I switch to using rexml?

Craig D. wrote:

On Sat, May 3, 2008 at 12:35 PM, James B. <
[email protected]> wrote:

With that code above, you’re trying to call the class method #to_xml on
Category. That method doesn’t exist. #to_xml is an instance method
inherited from ActiveRecord::Base.

Regards,
Craig

SO, I should move my code to the controller where I have access to each
instance?

On Sat, May 3, 2008 at 12:35 PM, James B. <
[email protected]> wrote:

undefined method ‘to_xml’ for class:0xb76bef9c

With that code above, you’re trying to call the class method #to_xml on
Category. That method doesn’t exist. #to_xml is an instance method
inherited
from ActiveRecord::Base. If you want to customize #to_xml, do something
like
this:

class Category < ActiveRecord::Base
has_many :entries

def to_xml
# your changes here
end
end

Regards,
Craig

Craig D. wrote:

def show
@category = Category.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @category.to_xml(:only => [:id, 

:name], :include => [:entries] ) }

Perfect! Thanks so much for the help!

On Sat, May 3, 2008 at 1:28 PM, James B. <
[email protected]> wrote:

SO, I should move my code to the controller where I have access to each
instance?

Yes, your controller would do something like this:

class CategoriesController < ApplicationController

def show
@category = Category.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @category.to_xml(:only => [:id, 

:name],
:include => [:entries] ) }
end
end

end

Regards,
Craig

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs