Created
August 23, 2010 20:37
-
-
Save scottwb/546281 to your computer and use it in GitHub Desktop.
Enumerate all Rails ActiveRecord::Base model classes.
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
# 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