asplake

Friday, December 30, 2005

Loading model classes with STI

I found out the hard way that the SQL generated by Single Table Inheritance (STI) depends on what Rails knows to be the subclasses of the class you attempt to query. If the subclasses don't happen to be loaded, the query doesn't return all the results you'd expect. The simple cure is to add dependencies in your ApplicationController. You can do this more than once, eg:
  class ApplicationController < ActionController::Base
    # Make sure that ActiveRecord is always aware of Entry's subclasses
    model :entry, :catalog, :root_catalog

    # Make sure that ActiveRecord is always aware of ElementDef's subclasses
    model :element_def, :ref_component_def
  end
Spookily, having decided to write this up here and (later tonight) on the Rails wiki at WhenToUseTheModelMethod, I see this raised as a bug today. See ticket 3358. Personally, I think this is a feature rather than a bug, but I suppose it is slightly weird that a superclass should be dependent on its subclasses. Technorati tag:

0 Comments:

Post a Comment

<< Home