5

Is there a way in the rails console to display all rails models?

Something like this:

Models.all

which results on this:

[Customer, Site, Page, Download]
gespinha
  • 7,968
  • 16
  • 57
  • 91
  • 2
    possible duplicate of [Is there a way to get a collection of all the Models in your Rails app?](http://stackoverflow.com/questions/516579/is-there-a-way-to-get-a-collection-of-all-the-models-in-your-rails-app) – Nicolas Maloeuvre Aug 14 '15 at 16:17

4 Answers4

14

Try this:

ActiveRecord::Base.subclasses

This will return an array, so to get the name of the models only, you'd need to run:

ActiveRecord::Base.subclasses.map(&:name)
neo
  • 4,078
  • 4
  • 25
  • 41
  • 2
    No need for `send`, `#subclasses` is public. – Kristján Aug 14 '15 at 15:58
  • 1
    you don't need `send` .. it is a public method – Arup Rakshit Aug 14 '15 at 15:58
  • Is there any abbreviation for the `ActiveRecord::Base` snippet? Everytime I need something from the base layer, do I need to use this? – gespinha Aug 14 '15 at 16:03
  • Does this actually work at all times? When I try this, some models are not included until I use them in the code, and it includes some non-model classes that apparently do something with `has_and_belongs_to_many`. For example, I get [`User`, `User::HABTM_Agencies`], then I invoke `Agency`, then I get [`User`, `User::HABTM_Agencies`, `Agency`, `Agency::HABTM_Users`] – eirikir Aug 14 '15 at 16:13
  • 2
    This only works with loaded files. So in a development environment you'll only get a subset of the models. This is not a problem in production since models are eagerloaded. A possible solution would be to run `Dir["#{Rails.root}/app/models/**/*.rb"].each {|f| puts f; require File.basename f}` prior to running subclasses. – boulder Aug 14 '15 at 16:19
  • The best answer is found in an answer to a very similar question, which works on Rails 3 to 6: https://stackoverflow.com/a/10712838/3577922 Note this accepted answer does not work on Rails 5+ (the question is tagged for Rails 4). – Masa Sakano Jul 04 '21 at 17:00
6

Just run:

Rails.application.eager_load!
ActiveRecord::Base.subclasses
nothing-special-here
  • 11,230
  • 13
  • 64
  • 94
3

I tried both the answers above, It does not work as expected. I got this result,

["User", "HABTM_Roles", "ApplicationRecord", "PublicActivity::ORM::ActiveRecord::Activity", "ApplicationRecordGlobal", "HABTM_Users", "UserSync"] 

What worked for me was,

Rails.application.eager_load!
ApplicationRecord.subclasses.map(&:name)
devs1993
  • 408
  • 4
  • 9
  • I think the origonal answer is for an older version of rails. I'm using rails `gem 'rails', '~> 5.0.5'` and this worked for me. Nice find! – Lex Nov 12 '19 at 23:42
1

if you run rails c with -e production, then

ApplicationRecord.subclasses
ApplicationRecord.subclasses.map(&:name)

if you run rails c in development, you need to run eager_load before above command

Rails.application.eager_load!

for all models have inherited from ApplicationRecord and ApplicationRecord inherited from ActiveRecord::Base. and eager_load! will load all Models, but in development environment eager_load are false. you can find this config in config/environments/development.rb

wukoo
  • 11
  • 3