wtorek, 6 lipca 2010

Rails 3 bug that occures when using inherited callback without calling super . Nasty error when scopes are called on STI classes.

In case you are using the inherited callback in Rails3, don't forget to call super.

class BaseAttribute < ActiveRecord::Base

def self.inherited(klass)
super

klass.base_attribute_class.class_eval do
extend MyModule
end
end

Otherwise STI might not work properly. In my case for every inherited class I was looking for another one based on their names and extending them with some module which was defining scopes. Unofortunatelly I was getting errors that some methods was called on NilClass when I tried to use those scopes. Calling the super helped. Another thing is that with out using
super
the method
subclasses
also works badly.