1

我的项目中有几个 ActiveResource 模型。当我在 ActiveResource 上调用 to_xml 时,我感到很奇怪。我传递给 to_xml 的选项,例如 :only 和 :except 根本不起作用。在 ActiveRecord 上,它工作得非常好。有谁知道?

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
4

3 回答 3

1

ActiveResource::Base#to_xml 的实现不同于 ActiveRecord::Base。

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

ActiveResource::Base#to_xml 只接受 :indent, :dasherize, :camelize and :skip_instruct.

于 2009-10-05T14:54:53.720 回答
1

和上的to_xml方法是独立的实现。这意味着您不能期望它们的行为完全相同或采用相同的论点。ActiveRecordActiveResource

于 2009-10-05T15:01:56.093 回答
0

你说“在这里之后,我仍然获得所有属性”。看起来你认为node.to_xml会改变它node本身,但事实并非如此。你所要做的

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

然后参考xml

于 2009-09-09T07:30:46.310 回答