single table inheritance in rails 2.1
With the new “rails 2.1” there was a small but for me important change in the STI that made my life easier. Thanks to divoxx for his enhancement. It didn’t show up in the docs yet but I’ll give you an example where.
The documentation’s example consists of the models Company, Firm and Client, very simple. My real life example (it will be on the new plasticpilots website) consists of a model Comment that I use as an association for Post and Site. So Post and Site have many comments. To save duplication I use STI of course and to keep the directories clean I have Site::Comment << Comment and Post::Comment << Comment (and maybe more in the far future of 2009). Old rails versions did a demodulize the class names so I would have Comment in my type column and a lot of confusion. With rails 2.1 it’s the full class name in the type column and I can kick all the work-arounds I had previously.
ActiveRecord::Base.store_full_sti_class = false
August 26th, 2008 at 05:44 AM
What’s the difference between a site comment and a post comment? Why can’t you just have a comments table with foreign keys going to both posts table and the sites table? comments: - id - post_id - site_id
September 4th, 2008 at 02:28 PM
Any chance of you writing a small tutorial on how to use the new STI behaviour?
September 14th, 2008 at 10:30 AM
@railsforum: Of course you could but it wouldn’t be that flexible like with sti. Actually there’s a acts_as_commentable which is setting a commentable_type just like the new STI does behave. Beside that there are uses like displaying all post comments which is getting nasty without STI.