Help with abstracting ActiveRecord models

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

On Tue, Aug 25, 2009 at 10:11 PM, Jeff Larkin[email protected]
wrote:

RSSWidget < Widget
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 D.
http://destiney.com/

On Aug 25, 2009, at 8:11 PM, Jeff Larkin wrote:

RSSWidget < Widget
Is there an AR hook that I can use immediately after the model is
-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.

On Wed, Aug 26, 2009 at 12:29 AM, s.ross[email protected] wrote:

Does that meet the criteria you’ve spelled out? Because if it does, it’s
been in Rails since before 1.0.

And if it doesn’t meet his criteria, when if ever did STI get into
Rails?


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

On Aug 26, 2009, at 6:47 AM, Rick DeNatale wrote:

Rick DeNatale
if rails.include? STI
puts “sigh of relief – must be before today”
else
puts “pain of embarrassment – must be wrong”
end

or…

if requirements =~ /STI/
puts “STI must have been included in Rails before 1.0”
else
puts “STI implementation date not computable from given information”
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs