Ruby on rails 序列化为xml时出现NoMethodError

Ruby on rails 序列化为xml时出现NoMethodError,ruby-on-rails,serialization,xml-serialization,Ruby On Rails,Serialization,Xml Serialization,我试图从数据库中获取一些模型对象并将它们序列化为xml,但是当包含一个特定的(有很多)关联时,我会得到以下错误。JSON序列化(to_JSON)可以正常工作(也包括属性)。还有一个对象(XML和JSON)的序列化。有什么问题吗?(模型类不覆盖任何XML序列化内容,我使用的是Rails 3) 下面是命令: Entity.all.to_xml:include=>:properties 和转储: NoMethodError: undefined method `macro' for nil:NilCl

我试图从数据库中获取一些模型对象并将它们序列化为xml,但是当包含一个特定的(有很多)关联时,我会得到以下错误。JSON序列化(to_JSON)可以正常工作(也包括属性)。还有一个对象(XML和JSON)的序列化。有什么问题吗?(模型类不覆盖任何XML序列化内容,我使用的是Rails 3)

下面是命令:
Entity.all.to_xml:include=>:properties

和转储:

NoMethodError: undefined method `macro' for nil:NilClass from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activesupport-3.0.1/lib/active_support/whiny_nil.rb:48:in `method_missing' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activerecord-3.0.1/lib/active_record/serialization.rb:41:in `serializable_add_includes' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activerecord-3.0.1/lib/active_record/serialization.rb:40:in `each' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activerecord-3.0.1/lib/active_record/serialization.rb:40:in `serializable_add_includes' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activerecord-3.0.1/lib/active_record/serializers/xml_serializer.rb:191:in `send' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activerecord-3.0.1/lib/active_record/serializers/xml_serializer.rb:191:in `add_includes' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activerecord-3.0.1/lib/active_record/serializers/xml_serializer.rb:186:in `add_extra_behavior' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activemodel-3.0.1/lib/active_model/serializers/xml.rb:103:in `serialize' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:31:in `tag!' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activemodel-3.0.1/lib/active_model/serializers/xml.rb:101:in `serialize' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activerecord-3.0.1/lib/active_record/serializers/xml_serializer.rb:175:in `to_xml' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activerecord-3.0.1/lib/active_record/associations/association_proxy.rb:218:in `send' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activerecord-3.0.1/lib/active_record/associations/association_proxy.rb:218:in `method_missing' ... 5 levels... from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:31:in `tag!' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activemodel-3.0.1/lib/active_model/serializers/xml.rb:101:in `serialize' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activerecord-3.0.1/lib/active_record/serializers/xml_serializer.rb:175:in `to_xml' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activesupport-3.0.1/lib/active_support/xml_mini.rb:107:in `to_tag' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activesupport-3.0.1/lib/active_support/core_ext/array/conversions.rb:159:in `to_xml' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activesupport-3.0.1/lib/active_support/core_ext/array/conversions.rb:159:in `each' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activesupport-3.0.1/lib/active_support/core_ext/array/conversions.rb:159:in `to_xml' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activesupport-3.0.1/lib/active_support/core_ext/array/conversions.rb:158:in `__send__' from /home/kai/.rvm/gems/ruby-1.8.7-p302@projectx/gems/activesupport-3.0.1/lib/active_support/core_ext/array/conversions.rb:158:in `to_xml' 实体迁移中存在由创建的
整数字段。似乎当您将“属于”关联命名为与:foreign\u键相同时,XML序列化就会中断。当我更改为
属于\u to:creator,:class\u name=>“User”,:foreign\u key=>“created\u by”
时,序列化将按预期工作

更新的更新的更新;-)
这已经在Rails 3.0.3中解决了(我有3.0.1)。那么我学到了什么。。。首先更新软件

模型关系很可能未正确设置。 您是否能够做到:

e = Entity.find.first e.properties e=实体.find.first e、 性质
并让它返回相关对象?

是的,它返回。正如我所提到的,相同的命令在相同的数据库状态下运行,但是只使用to_json就可以了(所有属性都正确地序列化为json)。 e = Entity.find.first e.properties