【问题标题】:Mongoid Inheritance descendants issue in ruby-debugruby-debug 中的 Mongoid 继承后代问题
【发布时间】:2011-10-29 23:57:52
【问题描述】:

我对 mongoid 继承有疑问。我有一个从 Location 类继承自两个 mongoid 文档的邮政编码。它具有以下简单的继承结构:

class Zipcode < Location

我还有一个使用nested_attributes 填充邮政编码对象的对象。我注意到,当它尝试创建 Zipcode 对象时,它反而创建了一个 Location 对象。在我的测试代码中,一切正常,但在我的生产代码中,一切都无法正常工作。

在我的代码上运行 ruby​​-debug 后,我在文件中发现了以下行为:~/work/Project/bson/ruby/1.9.1/gems/mongoid-2.2.3/lib/mongoid/document.rb :277

Breakpoint 2 at /Users/pc/work/Project/bson/ruby/1.9.1/gems/mongoid-2.2.3/lib/mongoid/document.rb:277
/Users/pc/work/Project/bson/ruby/1.9.1/gems/mongoid-2.2.3/lib/mongoid/document.rb:277
@_type ||= [descendants + [self]].flatten.uniq.map { |t| t.to_s }
(rdb:48) p descendants
[]
(rdb:48) p Zipcode.ancestors && [Location]
[Location]
(rdb:48) p descendants
[Zipcode]
(rdb:48)

看到在调用Zipcode.ancestors 时填充后代(在 BasicObject 中)很奇怪。我正在使用 Ruby1.9.2、Mongoid 2.2.3 和 Rails3.1.1。

问题:在嵌套属性中获取继承的 mongoid 语法有什么特别之处吗?是否存在需要在 mongoid、rails 或 ruby​​ 中修复的问题?

更新:

只是为了证明第一次闯入测试代码是有效的:

Breakpoint 1 at /Users/pc/work/Project/bson/ruby/1.9.1/gems/mongoid-2.2.3/lib/mongoid/document.rb:277
/Users/pc/work/Project/bson/ruby/1.9.1/gems/mongoid-2.2.3/lib/mongoid/document.rb:277
@_type ||= [descendants + [self]].flatten.uniq.map { |t| t.to_s }
(rdb:1) p descendants
[Zipcode]

【问题讨论】:

    标签: ruby-on-rails-3 inheritance mongoid nested-attributes ruby-1.9


    【解决方案1】:

    发现解决方案必须改变

    config.cache_classes = true 
    

    在 config/environments/development.rb 中。它有时显然会混淆 Rails 中的继承。

    【讨论】:

      猜你喜欢
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多