【问题标题】:Preventing Inheritance in MongoMapper在 MongoMapper 中防止继承
【发布时间】:2011-11-11 17:15:37
【问题描述】:

我正在使用带有 MongoDB 和 MongoMapper 的 Rails。我的问题是我有一个从另一个继承的类,我想省略其中一个键。例如:

class A
    include MongoMapper::EmbeddedDocument
    many :items
    #Other keys I want
end

class Item < A
    include MongoMapper::EmbeddedDocument
    #Included Keys from A
    #Other Keys that I want
end

这里的问题是 Item 继承了许多 :items 的 A 的关系。我怎样才能防止这种情况发生?

【问题讨论】:

    标签: ruby ruby-on-rails-3 inheritance mongodb mongomapper


    【解决方案1】:

    这个:

    从另一个继承的类,我想省略其中一个键

    表示您没有有效的继承关系。也许你想要更多这样的东西:

    class B
        # Common things for A and C
    end
    
    class A < B
        many :items
        # Other things that shouldn't be in B or C
    end
    
    class C < B
        # Other keys you want that aren't already in B
    end
    

    试图缩小派生类的接口表明你做错了什么,需要重新考虑你的层次结构。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      相关资源
      最近更新 更多