On Tue, Aug 25, 2009 at 10:11 PM, Jeff Larkin<[email protected]> 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.
It seems to me you're thinking in Java or C++. Ruby isn't about what type or class an object is based on so much as what the object can do. I don't see very much object casting when I read other people's Ruby. Mostly it's just the primitives, string to ints, etc. Google "ruby duck typing", and check out respond_to? and module mixins. -- Greg Donald http://destiney.com/ --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

