Rails STI overrides the model name in the parent class for all subclasses

I use STI in a Rails application and in order not to define routes for all subclasses, I put the following in each subclass:

def self.model_name
  Mapping.model_name
end

In the above example Mapping, this is the name of the parent model. Example:

class UserMapping < Mapping; end

If you put this in each subclass is not very DRY, so I'm looking for a way to set this in the parent somehow so that every class that inherits from the parent automatically has a model name set as the parent model name.

Perhaps there is an even better way to overcome the routing problem that arises from a non-installation STI model_name- I'm open to suggestions!

Thanks in advance!

+4
1

Mapping:

class Mapping < ActiveRecord::Base
  def self.inherited(subclass)
    super
    def subclass.model_name
      superclass.model_name
    end
  end
end

Mapping model_name.

+5

All Articles