Search code examples
ruby-on-railsruby-on-rails-3sti

Deprecation warning with STI model_name workaround


In my Single Table Inheritance models, I am overriding the inherited method in the base model so that all descendent models are recognized by the base model's name. The following code is used to add an override to the model_name method for all inherited classes.

  def self.inherited(child)
    child.instance_eval do
      def model_name
        BaesModelDefinition.model_name
      end
    end
  end

I've noticed that this is producing deprecation warnings in Rails 3.2.3:

DEPRECATION WARNING: It looks like something (probably a gem/plugin) is overriding 
the ActiveRecord::Base.inherited method. It is important that this hook executes so 
that your models are set up correctly. A workaround has been added to stop this 
causing an error in 3.2, but future versions will simply not work if the hook is
overridden.

Is there another approach I can use to fix the model_name issue?


Solution

  • The answer turned out to be simple. Just add a super to the overriding method.

      def self.inherited(child)
        child.instance_eval do
          def model_name
            BaesModelDefinition.model_name
          end
        end
        super
      end