Skip to content

Instantly share code, notes, and snippets.

@scottwb
Created August 23, 2010 20:37
Show Gist options
  • Save scottwb/546281 to your computer and use it in GitHub Desktop.
Save scottwb/546281 to your computer and use it in GitHub Desktop.
Enumerate all Rails ActiveRecord::Base model classes.
# Add AR::Base class methods for enumerating all the model
# classes in a Rails project.
module ActiveRecord
class Base
def self.all_model_classes
connection.tables.map do |table|
begin
klass = Class.const_get(table.classify)
klass.ancestors.include?(self) ? klass : nil
rescue
nil
end
end.compact
end
def self.each_model_class(&block)
all_model_classes.each(&block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment