Created
January 7, 2010 17:46
-
-
Save tobstarr/271396 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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