2008-01-13

the long-awaited Rails STI tutorial

So, as I promised all of you, my dear readers, I am finally commencing to publish my tutorial for single table inheritance in Ruby on Rails.

Don't use it. I should have been smart enough to realize that I wasn't the first or the smartest person to recognize a need for a Rails STI tutorial. What I found, after many painful hours of searching and experimenting, can be summarized by the following:
  • Rails 1.2.0, upon which my particular little pet project is running, at best offers the feature with all the warmth and plug-and-play ease of an Italian range stove. Hell, if A New Hope is any indicator, even the Death Star provides an easier API. Where's my script/generate sti_model love?
  • Rails 2 may solve this; I haven't looked over it yet
  • The documentation on this feature consists of an even thousand attempts much more valiant than my own - all blog entries like this one - of which about 992 are copies of the original 7. The only 1 that actually has all the answers was written by Kurt Vonnegut who was planning to publish on a Thursday exactly 5 weeks before the start of RailsConf 2007. Alas.
  • The "We're Sorry, But Something Went Wrong" message is so polite, so nice and creamy colored, like a banana split in a brittle glass bowl, sitting del.icio.us, inviting, and cold in a concrete room.
Instead I made a column in my Tasks table called 'flavor' in which I store values like 'SendemailTask', 'PhonecallTask', 'Neopolitan'. Then when I have a method that needs to be subclassed, I just use the following sort of thing:

def do_stuff
    mymethod = self.method('do_stuff_for_flavor_'+flavor)
    mymethod.call
end

def do_stuff_for_flavor_Neopolitan
    if banana?
        for stripe in ['left', 'middle', 'right']
            scoop = stripe.get_scoop
            bowl.push(scoop)
        end
        bowl.push(get_banana)
        bowl
    else
        raise "no banana! wtf?"
    end
end


No comments: