序列化to_xml时的NoMethodError

时间:2010-11-18 17:51:04

标签: ruby-on-rails serialization xml-serialization

我尝试从数据库中获取一些模型对象并将它们序列化为xml,但是当包含特定的(has_many)关联时,我得到以下错误。 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'

更新和解决方案

HoBlend是对的......实体模型中的一个关联设置不正确,但没有properties一个。 在实体中,还存在与用户模型的关联:
belongs_to :created_by, :class_name => "User", :foreign_key => "created_by"
如果您习惯使用ActiveRecord关联,则会看到:foreign_key参数在此处无效(它必须位于关联的另一侧,它也是如此)。 Rails从不抱怨这个参数,我的测试都通过了。但它使XML序列化以某种方式失败(令人惊讶的不是JSON序列化)。

更新更新

似乎ActiveRecord中存在某个错误。想想以下模型:

class User < ActiveRecord::Base
  has_many :created_entities, :class_name => "Entity", :foreign_key => "created_by"
end

class Entity < ActiveRecord::Base
  belongs_to :created_by, :class_name => "User", :foreign_key => "created_by"
end

实体迁移中存在created_by整数字段。当您将belongs_to关联命名为:foreign_key时,似乎XML序列化会中断。一旦我更改为belongs_to :creator, :class_name => "User", :foreign_key => "created_by",序列化就会按预期工作。

更新更新的更新; - )

这已经在Rails 3.0.3中解决了(我有3.0.1)。所以我该学到什么......首先更新软件!

1 个答案:

答案 0 :(得分:1)

很可能模型关系设置不正确。 你能做到:

e = Entity.find.first
e.properties

并让它返回关联的对象?