Skip to content

Instantly share code, notes, and snippets.

@ujackson
Forked from equivalent/list_all_rails_models.rb
Created January 12, 2023 11:04
Show Gist options
  • Save ujackson/7435bd151c9ee72fcc999f8aefe3e0ab to your computer and use it in GitHub Desktop.
Save ujackson/7435bd151c9ee72fcc999f8aefe3e0ab 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment