inherited(p1) public

Callback invoked whenever a subclass of the current class is created.

Example:

   class Foo
      def self.inherited(subclass)
         puts "New subclass: #{subclass}"
      end
   end

   class Bar < Foo
   end

   class Baz < Bar
   end

produces:

   New subclass: Bar
   New subclass: Baz
Show source
Register or log in to add new notes.
January 22, 2010
1 thank

Interesting usage for polymorphic asset model :)

…to automatically define default scopes of inherited classes.

 class Asset < ActiveRecord::Base

  belongs_to :resource, :polymorphic => true
  before_save :set_asset_type

  def set_asset_type
   self.asset_type = self.class.name
  end

  def self.inherited(subclass)
    super
    subclass.send(:default_scope, :conditions => "asset_type='#{subclass.name}'")
  end
end