开发者

Inheritance with MongoMapper: searching the parent class

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 t开发者_Python百科he class name in the db. does this make sense, or is there a better way?


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:

key :_type, String
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜