Skip to content

Instantly share code, notes, and snippets.

@equivalent
Created November 8, 2011 13:03
Show Gist options
  • Save equivalent/1347687 to your computer and use it in GitHub Desktop.
Save equivalent/1347687 to your computer and use it in GitHub Desktop.
how to get model names of all rails models (works for STI)
#Since Rails doesn't load classes unless it needs them, you must read the models from the folder. Here is the code
Dir[Rails.root.to_s + '/app/models/**/*.rb'].each do |file|
begin
require file
rescue
end
end
models = ActiveRecord::Base.subclasses.collect { |type| type.name }.sort
models.each do |model|
print model
print ' '
end
@DmytroVasin
Copy link

DmytroVasin commented Mar 26, 2017

http://apidock.com/rails/Class/descendants, Rails 5 uses abstract ApplicationRecord class
Or something like that:

root_klass = defined?(ApplicationRecord) ? ApplicationRecord : ActiveRecord::Base
root_klass.subclasses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment