On Tue, Jan 17, 2006 at 08:42:51AM -0000, Jonathan T. wrote:
I’m currently coding a system which must store multiple contact methods for
a user (phone, email, postal address etc). I’m planning a fairly
straightforward inheritance hierachy for these, where each different method
inherits from something like a ContactMethod class.
The only mention of ActiveRecord support for inheritance I can find in the
Wiki and the Agile Web Dev book is STI. What’s not clear is whether STI is
the only way that this will work. If I define my classes to have
inheritance relationships and then create a separate table for each subclass
will it not work?
I tried this over the weekend, thinking that it wouldn’t be huge issue.
What I had was a fairly simple hierarchy, like this:
User
/ \
/ \
Admin Customer
Both Admins and Customers could log in, so the User class has all the
common
stuff (login, password changing, etc), but there’s no table for User.
It’s this last part that caused the problem for me. The moment I tried
to
run something on the User class (even if the method had been overridden
by
me), the User class attempted to access the table users and it all fell
apart.
I think that if you had a table behind your base class, it might work,
but
that seems pretty pointless.
The way I sorted the problem was to create a User class, containing all
of
the class methods, and a UserMixin module, for the benefit of the
“subclasses”. It’s a pity that it needs to be done this way, and I
fully
agree that it’s not the most OO way of doing things, but if it becomes a
common, standard idiom, it won’t be a major problem.
Of course, if we really want to support inheritance, then we want to
throw
out this ORM business and go to a real object database…