method

inherited

v2_1_10 - Show latest stable - Class: Class
inherited(p1)
private

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

1Note

Interesting usage for polymorphic asset model :)

stevo ยท Jan 22, 20101 thank

...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