views:

196

answers:

1

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?

+1  A: 

you might want to take a look at this stackoverflow thread: http://stackoverflow.com/questions/2002235/mongomapper-parent-inheritance

actually you use "_type" as the key name, mongomapper automatically adds the class name to the entry:

key :_type, String
z3cko
thanks for the answer. I was actually more asking if it is a good practice from a design perspective, but it is an answer, so i accepted it.
railsmongo