使用MongoMapper继承:搜索父类
当使用 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 对象。 目前还不清楚它们是什么类型的物体。如果我们想显示一个 图标或类似的东西来区分各个项目, 可以通过将类名保存在数据库中来完成。这有道理吗 或者有更好的方法吗?
does it make sense to save the class name in a field when using inheritance with 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
if a search for Item is performed, MongoMapper would return Item Objects.
it is not clear, which kind of objects they are. if we want to display an
icon or something similar to distinguish the items from each other,
it could be done by saving the class name in the db. does this make sense,
or is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可能想看看这个stackoverflow线程: MongoMapper Parent Inheritance
实际上你使用“_type”作为键名,mongomapper 会自动将类名添加到条目中:
you might want to take a look at this stackoverflow thread: MongoMapper Parent Inheritance
actually you use "_type" as the key name, mongomapper automatically adds the class name to the entry: