On 26 Sep 2008, at 15:16, Mark Wilden wrote:
Also why is the article so down on STI? What are the drawbacks? What
do people use instead?
One downside to STI is it forces you to leave NULL columns for
attributes that don't exist in all models. This is also really bad
for integrity. I once started a CTI[1] extension to AR[2][3]. A lot
of users were interested in it, but I got no response from the core
team.
There's talk on doing it in DataMapper too. If I get some free time I
might look at it, but it's a fairly complex bit of ORM work. (I did
want to write an ORM but I spend way too much time doing paid work...)
I think the guy is really just down on inheritance itself, which is
not an unusual nor even entirely unjustified attitude. Ruby has far
less need to use inheritance because of the ability to mix in modules.
...
But I do believe that where a true "is-a" relationship exists,
inheritance is often the best approach. I'm working on a program
right now that has a Task class and an Appointment subclass. An
Appointment (in this context) is simply a Task that can only be
performed on one day. Otherwise it's exactly like a Task (again, in
the context of my program). That's an "is-a" relationship that
inheritance was designed for, and it's working quite well. And, as
an agile practitioner, if I find that this relationship changes I
would throw out this class hierarchy.
That sounds like a perfect example. I assume any code that works for
"Task" will happily process an appointment.
STI is just a way to map inheritance to the database. I used it
before I knew what it was called. If you do use inheritance with
models, I think it's the best Rails way to persist the data.
Not the best possible way (which is CTI), but yeah, the best way you
can do it with Rails (ActiveRecord). It's still a data modelling/
integrity headache, but not on the same level as polymorphic
associations.
Ashley
[1] http://martinfowler.com/eaaCatalog/classTableInheritance.html
[2] http://dev.rubyonrails.org/ticket/6566
[3] http://aviewfromafar.net/2008/1/19/class-table-inheritance-in-activerecord
[4] http://wm.lighthouseapp.com/projects/4819/tickets/53-class-table-inheritance
([3] was started on my old employer's blog, but they never maintained
it after I left)
--
http://www.patchspace.co.uk/
http://aviewfromafar.net/
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users