【问题标题】:Inheritance with MongoMapper: searching the parent classMongoMapper 的继承:搜索父类
【发布时间】:2011-01-07 09:21:35
【问题描述】:

在使用 mongomapper/rails 继承时将类名保存在字段中是否有意义?

class Item
  include MongoMapper::Document
  timestamps!
  key :class, String # does this actually make sense?
  key :title, String
end

class Post < Item
  key :body1, String
end

class Page < Item
  key :body2, String  
end

如果执行了对 Item 的搜索,MongoMapper 将返回 Item Objects。 目前尚不清楚,它们是哪种物体。如果我们想显示一个 图标或类似的东西来区分项目, 可以通过将类名保存在数据库中来完成。这有意义吗, 还是有更好的方法?

【问题讨论】:

    标签: ruby-on-rails ruby mongodb mongomapper


    【解决方案1】:

    你可能想看看这个 stackoverflow 线程:MongoMapper Parent Inheritance

    实际上你使用“_type”作为键名,mongomapper会自动将类名添加到条目中:

    key :_type, String
    

    【讨论】:

    • 感谢您的回答。实际上我更多的是从设计的角度询问这是否是一个好的做法,但这是一个答案,所以我接受了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2015-03-14
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多