On Aug 25, 2009, at 8:11 PM, Jeff Larkin wrote: > > Hi all. I'm looking for suggestions of how other people have > implemented a situation like the following. I'd like to create a base > activerecord model, let's call it Widget, from which other classes are > based. I'd like to store the name of the final class in the database > and either use that information to load the proper class or to include > a module that overrides certain methods of the base class. For > example: > > Widget < AR: Base > RSSWidget < Widget > HTMLWidget < Widget > > w = Widget.first > > Currently w is a generic widget; it doesn't know if it's really a > RSSWidget or an HTMLWidget. Really, what I'm more interest in is if > something, say Page, has many widgets, then I can iterate through all > widgets for an instance of page and each will cast itself to the > appropriate subclass (or include the proper module). > > Is there an AR hook that I can use immediately after the model is > loaded from the database to include the necessary module and either > deserialize any additional information or load additional properties > from other tables? I've read that AR has an abstract_class property, > but it almost looks like it sees this problem from the other > direction, with the tables belonging to the inherited classes, but > maybe I've misunderstood it. > > Any suggestions of how this may be done would be greatly appreciated. > > Thanks, > -Jeff
Look up single table inheritance. It, by definition and implementation, stores the name of the class in a database field. The syntax for declaration is simply inheritance, so nothing special there. The only requirement is that all database fields for the subclasses be present in the table for the base class. Best of all, when you do: w = RSSWidget.first w.class == RSSWidget => true Does that meet the criteria you've spelled out? Because if it does, it's been in Rails since before 1.0. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---