views:

326

answers:

3

I have several ActiveResource model in my project. There was so strange to me when I called to_xml on my ActiveResource. The options that I passed to to_xml like :only and :except doesn't work at all. On ActiveRecord, it works really well. Anyone knows?

class Node < ActiveResource::Base
   self.site = NODE_SERVER
end

# node has uuid, name, type attributes
node = Node.find("3333")
node.to_xml(:only => [:uuid])

# after here, i still get all attributes
A: 

You say "after here, I still get all attributes". It looks like you think node.to_xml will change the node itself, but that's not the case. You have to do

xml = node.to_xml(:only => [:uuid])

and then refer to xml.

August Lilleaas
no. I mean the to_xml still returns all attributes. Did you try that yet?
Chamnap
+1  A: 

The implementation of ActiveResource::Base#to_xml is different than ActiveRecord::Base.

See http://api.rubyonrails.org/classes/ActiveResource/Base.html#M000914

ActiveResource::Base#to_xml only accepts :indent, :dasherize, :camelize and :skip_instruct.

bartzon
I don't think the implementation is different. From my point of view, it would just called attributes.to_xml (which is a hash object) of a activeresource object. In addition, to_xml comes with ActiveSupport. If you try to call to_xml on a hash, you would see, it doesn't work either.
Chamnap
No, the implementation is different. ARecord::Base.to_xml uses XmlSerializer, whereas AResource::Base.to_xml uses a completely different method of serializing. Additionally, both implementation accept different options, as specified in the Rails API documentation. In other words, RTFM :).
bartzon
You're right. I just looked through code yesterday.
Chamnap
+1  A: 

The to_xml methods on ActiveRecord and ActiveResource are independent implementations. That means that you cannot expect them to behave exactly the same or take the same arguments.

Peter Wagenet