Skip to content

Instantly share code, notes, and snippets.

@tobstarr
Created January 7, 2010 17:46
Show Gist options
  • Save tobstarr/271396 to your computer and use it in GitHub Desktop.
Save tobstarr/271396 to your computer and use it in GitHub Desktop.
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :type
t.string :email
t.timestamps
end
end
def self.down
drop_table :users
end
end
# user.rb
class User < ActiveRecord::Base
end
# standard_user.rb
class StandardUser < User
end
# admin_user.rb
class AdminUser < StandardUser
end
# script/console
>> StandardUser.count
# log/development.log
# SELECT count(*) AS count_all FROM "users" WHERE ( ("users"."type" = 'StandardUser' ) )
>> StandardUser.send(:subclasses).map(&:name)
=> []
>> AdminUser # just force to load the class
=> AdminUser(id: integer, type: string, email: string, created_at: datetime, updated_at: datetime)
>> StandardUser.send(:subclasses).map(&:name)
=> ["AdminUser"]
>> StandardUser.count
# log/development.log
# SELECT count(*) AS count_all FROM "users" WHERE ( ("users"."type" = 'StandardUser' OR "users"."type" = 'AdminUser' ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment