Not logged in (Log in or Sign up)


STI(ck) to it

Single Table Inheritance is a handy feature of rails to take advantage of. Unfortunately, there are many pitfalls waiting for you should you choose to use it.

One of these (pitfalls), is that the column used by default for the type of the record is... "type". In case you haven't figured it out yet, the problem is that "type" is also a deprecated synonym for the Object#class method.

So, if you try to put the type attribute on your form you will have problems (when type returns the class for the model instead of the name of the class). You'll get something like...

wrong argument type String (expected Module)

Working around this is easy. Just shove the following code into an initializer...

   1  # type is a deprecated synonym for Object#class,
   2  # but it gets in the way of accessing the STI inheritance column,
   3  # so undefine it
   4  Object.class_eval "undef :type"
blog comments powered by Disqus