Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top