DEV Community 👩‍💻👨‍💻


Posted on

Get the list of classes connected to the DB

It was quite troublesome before Rails4, but after Rails5
You can get a list of models connected to DB in ApplicationRecord.descendants.

For example, something like this.

model_list = ApplicationRecord.descendants

# If you want to output a list of class names { |v| v.to_s }
=> ["User", "Owner", "Blogs::Comment"]

Usage scenarios

  • Batch processing
  • If you are looking for a method that can be executed by the method xxx

Top comments (0)

Become a Moderator Do you want us to help make DEV a better place?

Fill out this survey and help us by becoming a tag moderator here at DEV.